python - ConnectionRefusedError: [Errno 111] Connection refused
問題描述
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)問,錯(cuò)在哪里?應(yīng)該該怎么改?
問題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關(guān)文章:
1. java - spring-data Jpa 不需要執(zhí)行save 語(yǔ)句,Set字段就可以自動(dòng)執(zhí)行保存的方法?求解2. html5 - 微信瀏覽器視頻播放失敗3. javascript - 這問題怎么處理額4. css3 - 為什么css里面要帶-moz-|-webkit-后又來一個(gè)不帶它們的5. 網(wǎng)頁(yè)爬蟲 - Python 爬蟲中如何處理驗(yàn)證碼?6. javascript - jQuery中l(wèi)ive事件在移動(dòng)微信端下沒有效果;代碼如下7. javascript - SuperSlide.js火狐不兼容怎么回事呢8. javascript - owl.carousel.js這個(gè)插件的原作者的網(wǎng)址是多少了?9. phpstady在win10上運(yùn)行10. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)
