一。基于udp协议的套接字
服务端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))
while True:
msg=input('<>').strip()
if len(msg)==0:break
client.send(msg.encode('utf-8'))
data=client.recv(1024)
print(data)
client.close()
客户端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #数据报协议-》udp
while True:
msg=input('>>: ').strip() #msg=''
client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
data,server_addr=client.recvfrom(1024)
print(data)
client.close()
二。基于socketserver模块实现tcp并发
服务端
import socketserver
class MyHandler(
服务端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))
while True:
msg=input('<>').strip()
if len(msg)==0:break
client.send(msg.encode('utf-8'))
data=client.recv(1024)
print(data)
client.close()
客户端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #数据报协议-》udp
while True:
msg=input('>>: ').strip() #msg=''
client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
data,server_addr=client.recvfrom(1024)
print(data)
client.close()
二。基于socketserver模块实现tcp并发
服务端
import socketserver
class MyHandler(