python - ConnectionRefusedError: [Errno 111] Connection refused
问题描述
Python3实现了一个简单的udp server和udp client。host指定为’localhost’时,在同一台机器上是运行正常的。
udpserver.py:
from socket import *HOST = ’localhost’PORT = 9999s = socket(AF_INET,SOCK_DGRAM)s.bind((HOST,PORT))print(’...waiting for message..’)while True:data,address = s.recvfrom(1024)print(data,address)s.sendto(’this is the UDP server’.encode(’utf-8’), address)s.close()
udpclient.py:
from socket import *HOST=’localhost’#HOST=’deque.me’PORT=9999s = socket(AF_INET,SOCK_DGRAM)s.connect((HOST,PORT))while True:message = input(’send message: ’)s.sendall(message.encode(’utf-8’))data = s.recv(1024)print(data)s.close()
如果将udpclient.py里的host改为'deque.me',程序会出现错误。
如果udpclient.py和udpserver.py运行在同一台机器上,也就是’deque.me’这台服务器上,错误如下:
ubuntu@VM-117-216-ubuntu:~/Shield/Py3$ python3 udpclient.py send message: testTraceback (most recent call last): File 'udpclient.py', line 12, in <module> data = s.recv(1024)ConnectionRefusedError: [Errno 111] Connection refused
如果把udpclietn.py放在另一台windows机器上执行,错误提示图下:
D:ShieldPy3>python udpclient.pysend message: testTraceback (most recent call last): File 'udpclient.py', line 11, in <module> data = s.recv(1024)ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
试了试将udpserver.py中的host改为’deque.me’和’115.159.29.211’(公网IP地址),均出现如下错误:
root@VM-117-216-ubuntu:~/Shield/Py3# python3 udpserver.pyTraceback (most recent call last): File 'udpserver.py', line 7, in <module> s.bind((HOST,PORT))OSError: [Errno 99] Cannot assign requested address
肯定的是’deque.me’是能正确解析到这Linux服务器的。请问,错在哪里?应该该怎么改?
问题解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相关文章:
1. android - 手机qq浏览器处理服务器端返回的数据时,数据格式有时为object有时为string是什么原因?2. java - 关于京东单点登录认证的问题3. 怎样用python给mp3添加封面?4. python - 图片爬虫时候遇到问题 urllib.request.urlretrieve 下载到指定文件夹不成功?5. javascript - es5中Object.seal()密封对象后,可以修改对象原型上的属性吗?6. 利用百度地图API定位及附件商家信息服务7. 怎么能做出标签切换页的效果,(文字内容随动)8. win10 python3.5 matplotlib使用报错9. (python)关于如何做到按win+R再输入文件文件名就可以运行?10. python - 用urllib抓取网页上的下载链接,目标文件是xls形式,但发现抓下来的xls是空表,里面只有一句报错信息,求帮助。

网公网安备