python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式
Posted Rogn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式相关的知识,希望对你有一定的参考价值。
先写服务端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(b‘welcome to server!‘) print(‘Connection success!‘) else: connection.send(b‘please 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(b‘1‘) 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的解决方式的主要内容,如果未能解决你的问题,请参考以下文章
linux下报错:error while loading shared libraries
mysql,密码正确的情况下报错,ERROR 1045 (28000): Access denied for user 'root'@'localhost' (usin
python安装pip的时候一路cd到该目录下报错Fatal error in launcher: Unable to create process using '"'