python - ConnectionRefusedError: [Errno 111] Connection refused
問(wèn)題描述
Python3實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的udp server和udp client。host指定為’localhost’時(shí),在同一臺(tái)機(jī)器上是運(yùn)行正常的。
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',程序會(huì)出現(xiàn)錯(cuò)誤。
如果udpclient.py和udpserver.py運(yùn)行在同一臺(tái)機(jī)器上,也就是’deque.me’這臺(tái)服務(wù)器上,錯(cuò)誤如下:
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放在另一臺(tái)windows機(jī)器上執(zhí)行,錯(cuò)誤提示圖下:
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] 遠(yuǎn)程主機(jī)強(qiáng)迫關(guān)閉了一個(gè)現(xiàn)有的連接。
試了試將udpserver.py中的host改為’deque.me’和’115.159.29.211’(公網(wǎng)IP地址),均出現(xiàn)如下錯(cuò)誤:
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服務(wù)器的。請(qǐng)問(wèn),錯(cuò)在哪里?應(yīng)該該怎么改?
問(wèn)題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問(wèn)題3. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪(fǎng)問(wèn)?4. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?5. css - 寫(xiě)頁(yè)面遇到個(gè)布局問(wèn)題,求大佬們幫解答,在線(xiàn)等,急!~6. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽(tīng)數(shù)據(jù)變化7. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤8. 實(shí)現(xiàn)bing搜索工具urlAPI提交9. phpstudy8.1支持win11系統(tǒng)嗎?10. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問(wèn)

網(wǎng)公網(wǎng)安備