python实现socket很简单,保证你的环境有响应的python环境就可以,我使用的是socket,demo代码如下:

server端程序:

 1 # coding:utf-8
 2 
 3 import socket   # 导入 socket 模块
 4 
 5 
 6 def test_server():
 7     s = socket.socket()  # 创建 socket 对象
 8 
 9     host = socket.gethostname()  # 获取本地主机名
10     port = 12345  # 设置端口
11     addr = (host, port)  # 设置地址tuple
12     s.bind(addr)  # 绑定端口
13 
14     s.listen(10)  # 等待客户端连接
15     while True:
16         c, addr = s.accept()  # 接收客户端的连接
17         print '连接地址:', addr
18         c.send('this is a test!')
19         c.close()  # 关闭连接

client端程序:

 1 # coding:utf-8
 2 
 3 import socket   # 导入 socket 模块
 4 
 5 def test_client():
 6     s = socket.socket()  # 创建 socket 对象
 7     host = socket.gethostname()  # 获取本地主机名
 8     port = 12345  # 设置端口
 9     addr = (host, port)
10     s.connect(addr)  # 绑定端口号
11     print s.recv(1024)  # 打印接收的数据
12     s.close()  # 关闭连接

代码执行结果:

server端:

python实现socket通信

client端:

python实现socket通信

 

相关文章:

  • 2020-05-23
  • 2021-07-05
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-12-19
  • 2021-07-07
  • 2022-12-23
猜你喜欢
  • 2021-07-31
  • 2021-08-15
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
相关资源
相似解决方案