码迷,mamicode.com
首页 > 编程语言 > 详细

python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式

时间:2019-12-03 20:00:40      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:bin   timeout   https   ted   add   nbsp   col   style   cti   

先写服务端server.py:

import socket
import time
 
HOST = 172.17.xx.xx  #服务器的私网IP
#HOST = ‘localhost‘
PORT = 8001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock.bind((HOST, PORT))  
sock.listen(5)  
while True:  
    connection,address = sock.accept()  
    try:  
        connection.settimeout(10)  
        buf = connection.recv(1024)  
        if buf:  
            connection.send(bwelcome to server!)
            print(Connection success!)
        else:  
            connection.send(bplease go out!)  
    except socket.timeout:  
        print (time out)
    connection.close()  
 

客户端client.py:

import socket
import time
 
#HOST = ‘localhost‘ 
HOST = 212.64.xx.xx  #服务器的公网IP
PORT = 8001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock.connect((HOST, PORT))  
time.sleep(2)  
sock.send(b1)
print (sock.recv(1024).decode())
sock.close()  

使用本地测试(即HOST=‘localhost‘)是可以的,但是在腾讯云/阿里云上报错“[Errno 99] Cannot assign requested address”,解决方法:服务端的ip填私网ip,客户端填公网ip。

 

 

参考链接:

1. https://blog.csdn.net/weixin_41656968/article/details/80042554

2. https://blog.csdn.net/weixin_43146445/article/details/93506761

python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式

标签:bin   timeout   https   ted   add   nbsp   col   style   cti   

原文地址:https://www.cnblogs.com/lfri/p/11978802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!