python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式
2024-09-03 05:30:50
先写服务端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'')
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
最新文章
- WCF学习之旅—HTTP双工模式(二十)
- 关于delphi7的四舍五入
- IIS 7.5 Application Warm-Up Module
- 兼容IE与firefox的css 线性渐变(linear-gradient)
- CSS Margin外边距合并
- oracle 11g 表空间使用率
- 给QT不规则窗口添加阴影
- web开发基础(同步更新中)
- Linux常用命令大全(2)
- select p.id, name,ROW_NUMBER() over(PARTITION by name order by p.id) names from person p
- haxe 中使用音效
- 老男孩Python全栈开发(92天全)视频教程 自学笔记20
- 20171201 - macOS High Sierra 神级 bug
- 根据Webservice地址,动态传入参数(Webservice代理类)
- Python练手例子(13)
- 2019年4月zstu月赛A: 我不会做
- Hibernate多对一ManytoOne
- Create and format Word documents using R software and Reporters package
- mysql基础知识(2)
- 微信小程序表单校验WxValidate.js使用
热门文章
- Octave Convolution详解
- swoole中http_server的配置与使用
- linux如何修改权限详解
- GO 函数的参数
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之七 || API项目整体搭建 6.2 轻量级ORM
- python基础(10):文件操作
- MySqlBulkLoader 中文乱码
- 使用原生Ajax进行用户名重复的检验
- 「杂谈」最有可能成为第五个一线城市,苏州 or 杭州?
- 在Dynamis CRM中打造一键保存关闭刷新案例的功能