基于socket实现简单套接字通信
服务端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
# 1.购买手机
phone= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.绑定号码
phone.bind(('192.168.1.113',8090))
# 3.设置接入数量
phone.listen(10)
# 4.等待接听
connt,clit = phone.accept()
# 5.收发数据
data = connt.recv(1024)
connt.send(data.upper())
print('接受到的数据',data)
# 6.关闭连接
connt.close()
phone.close()
客户端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
# 1.购买手机
phone1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.要拨打的号码
phone1.connect(('192.168.1.113',8090))
# 3.发送接收数据
phone1.send('hello'.encode('utf-8'))
data1 =phone1.recv(1024)
print(data1)
# 4.关闭连接
phone1.close()