【问题标题】:Error trying to send message to serve Ubuntu 18.04 LTS尝试发送消息以服务 Ubuntu 18.04 LTS 时出错
【发布时间】:2021-01-30 09:34:16
【问题描述】:

当我尝试将带引号的字符串从客户端发送到服务器时,它可以工作。但是,当我尝试将存储用户输入的变量发送到服务器时,它没有。有人知道为什么吗?

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

【问题讨论】:

    标签: server udp client ubuntu-18.04


    【解决方案1】:

    来自the documentation:

    输入([提示])
    等价于 eval(raw_input(prompt))。

    因此它将读取您输入的字符串(在本例中为ionut),然后对其进行评估。由于ionut 不是声明的变量或其他有效的 Python 语句,它会抛出所示的错误。

    也来自文档:

    考虑将 raw_input() 函数用于用户的一般输入。

    这是你应该改用的,然后你不会得到错误。

    除此之外,请考虑使用 Python3 而不是您当前使用的 Python2。 Python2 即将结束,Python3 中的 input 函数更符合您的预期 - 请参阅 this documentation。

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2017-01-19
      • 1970-01-01
      • 2018-05-12
      • 2021-07-26
      • 2020-12-05
      相关资源
      最近更新 更多