先写服务端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

最新文章

  1. WCF学习之旅—HTTP双工模式(二十)
  2. 关于delphi7的四舍五入
  3. IIS 7.5 Application Warm-Up Module
  4. 兼容IE与firefox的css 线性渐变(linear-gradient)
  5. CSS Margin外边距合并
  6. oracle 11g 表空间使用率
  7. 给QT不规则窗口添加阴影
  8. web开发基础(同步更新中)
  9. Linux常用命令大全(2)
  10. select p.id, name,ROW_NUMBER() over(PARTITION by name order by p.id) names from person p
  11. haxe 中使用音效
  12. 老男孩Python全栈开发(92天全)视频教程 自学笔记20
  13. 20171201 - macOS High Sierra 神级 bug
  14. 根据Webservice地址,动态传入参数(Webservice代理类)
  15. Python练手例子(13)
  16. 2019年4月zstu月赛A: 我不会做
  17. Hibernate多对一ManytoOne
  18. Create and format Word documents using R software and Reporters package
  19. mysql基础知识(2)
  20. 微信小程序表单校验WxValidate.js使用

热门文章

  1. Octave Convolution详解
  2. swoole中http_server的配置与使用
  3. linux如何修改权限详解
  4. GO 函数的参数
  5. Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之七 || API项目整体搭建 6.2 轻量级ORM
  6. python基础(10):文件操作
  7. MySqlBulkLoader 中文乱码
  8. 使用原生Ajax进行用户名重复的检验
  9. 「杂谈」最有可能成为第五个一线城市,苏州 or 杭州?
  10. 在Dynamis CRM中打造一键保存关闭刷新案例的功能