尝试发送消息以服务 Ubuntu 18.04 LTS 时出错
Error trying to send message to serve Ubuntu 18.04 LTS
当我尝试从客户端向服务器发送带引号的字符串时,它起作用了。但是,当我尝试将存储用户输入的变量发送到服务器时,它不会。有人知道为什么吗?
server_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind( ("0.0.0.0", 1234) )
buff, addr = s.recvfrom(100)
print buff, addr
client_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nume_user = input()
# s.sendto( nume_user, ("127.0.0.1", 1234) ) # this does not work
s.sendto("john", ("127.0.0.1", 1234) ) # this works
buff, addr = s.recvfrom(100)
print buff
这是我遇到的错误 (Ubuntu 18.04 LTS)
Traceback (most recent call last:
File "c4-1.py", line 5, in <module>
nume_user = input()
File "<string>", line 1, in <module>
NameError: name 'ionut' is not defined
input([prompt])
Equivalent to eval(raw_input(prompt)).
因此它将读取您输入的字符串(在本例中为 ionut
),然后对其求值。由于 ionut
不是声明的变量或其他有效的 Python 语句,它将抛出显示的错误。
也来自文档:
Consider using the raw_input() function for general input from users.
这是您应该使用的,这样您就不会收到错误。
除此之外,请考虑使用 Python3 而不是您当前使用的 Python2。 Python2 是生命的终结,而且 Python3 中的 input
函数更符合您的预期 - 请参阅 this documentation.
当我尝试从客户端向服务器发送带引号的字符串时,它起作用了。但是,当我尝试将存储用户输入的变量发送到服务器时,它不会。有人知道为什么吗?
server_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind( ("0.0.0.0", 1234) )
buff, addr = s.recvfrom(100)
print buff, addr
client_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nume_user = input()
# s.sendto( nume_user, ("127.0.0.1", 1234) ) # this does not work
s.sendto("john", ("127.0.0.1", 1234) ) # this works
buff, addr = s.recvfrom(100)
print buff
这是我遇到的错误 (Ubuntu 18.04 LTS)
Traceback (most recent call last:
File "c4-1.py", line 5, in <module>
nume_user = input()
File "<string>", line 1, in <module>
NameError: name 'ionut' is not defined
input([prompt])
Equivalent to eval(raw_input(prompt)).
因此它将读取您输入的字符串(在本例中为 ionut
),然后对其求值。由于 ionut
不是声明的变量或其他有效的 Python 语句,它将抛出显示的错误。
也来自文档:
Consider using the raw_input() function for general input from users.
这是您应该使用的,这样您就不会收到错误。
除此之外,请考虑使用 Python3 而不是您当前使用的 Python2。 Python2 是生命的终结,而且 Python3 中的 input
函数更符合您的预期 - 请参阅 this documentation.