Python Socket学习
本文内容纲要:
Python在网络通讯方面功能强大,学习一下Socket通讯的基本方式
UDP通讯:
Server:
importsocket
port=8081
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#从指定的端口,从任何发送者,接收UDP数据
s.bind(('',port))
print('正在等待接入...')
whileTrue:
#接收一个数据
data,addr=s.recvfrom(1024)
print('Received:',data,'from',addr)
Client:
importsocket
port=8081
host='localhost'
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(b'hello,thisisatestinfo!',(host,port))
很简单。下面是TCP方式:
Server:
#-*-coding:utf-8-*-
fromsocketimport*
fromtimeimportctime
HOST=''
PORT=12345
BUFSIZ=1024
ADDR=(HOST,PORT)
sock=socket(AF_INET,SOCK_STREAM)
sock.bind(ADDR)
sock.listen(5)
whileTrue:
print('waitingforconnection')
tcpClientSock,addr=sock.accept()
print('connectfrom',addr)
whileTrue:
try:
data=tcpClientSock.recv(BUFSIZ)
except:
print(e)
tcpClientSock.close()
break
ifnotdata:
break
s='Hi,yousendme:[%s]%s'%(ctime(),data.decode('utf8'))
tcpClientSock.send(s.encode('utf8'))
print([ctime()],':',data.decode('utf8'))
tcpClientSock.close()
sock.close()
Client:
#-*-coding:utf-8-*-
fromsocketimport*
classTcpClient:
HOST='127.0.0.1'
PORT=12345
BUFSIZ=1024
ADDR=(HOST,PORT)
def__init__(self):
self.client=socket(AF_INET,SOCK_STREAM)
self.client.connect(self.ADDR)
whileTrue:
data=input('>')
ifnotdata:
break
self.client.send(data.encode('utf8'))
data=self.client.recv(self.BUFSIZ)
ifnotdata:
break
print(data.decode('utf8'))
if__name__=='__main__':
client=TcpClient()
上面的TCP方式有个问题,不能退出,好吧,我们改造一下,使这个程序可以发送quit命令以退出:
Server:
#-*-coding:utf-8-*-
fromsocketimport*
fromtimeimportctime
fromtimeimportlocaltime
importtime
HOST=''
PORT=1122#设置侦听端口
BUFSIZ=1024
ADDR=(HOST,PORT)
sock=socket(AF_INET,SOCK_STREAM)
sock.bind(ADDR)
sock.listen(5)
#设置退出条件
STOP_CHAT=False
whilenotSTOP_CHAT:
print('等待接入,侦听端口:%d'%(PORT))
tcpClientSock,addr=sock.accept()
print('接受连接,客户端地址:',addr)
whileTrue:
try:
data=tcpClientSock.recv(BUFSIZ)
except:
#print(e)
tcpClientSock.close()
break
ifnotdata:
break
#python3使用bytes,所以要进行编码
#s='%s发送给我的信息是:[%s]%s'%(addr[0],ctime(),data.decode('utf8'))
#对日期进行一下格式化
ISOTIMEFORMAT='%Y-%m-%d%X'
stime=time.strftime(ISOTIMEFORMAT,localtime())
s='%s发送给我的信息是:%s'%(addr[0],data.decode('utf8'))
tcpClientSock.send(s.encode('utf8'))
print([stime],':',data.decode('utf8'))
#如果输入quit(忽略大小写),则程序退出
STOP_CHAT=(data.decode('utf8').upper()=="QUIT")
ifSTOP_CHAT:
break
tcpClientSock.close()
sock.close()
Client:
#-*-coding:utf-8-*-
fromsocketimport*
classTcpClient:
#测试,连接本机
HOST='127.0.0.1'
#设置侦听端口
PORT=1122
BUFSIZ=1024
ADDR=(HOST,PORT)
def__init__(self):
self.client=socket(AF_INET,SOCK_STREAM)
self.client.connect(self.ADDR)
whileTrue:
data=input('>')
ifnotdata:
break
#python3传递的是bytes,所以要编码
self.client.send(data.encode('utf8'))
print('发送信息到%s:%s'%(self.HOST,data))
ifdata.upper()=="QUIT":
break
data=self.client.recv(self.BUFSIZ)
ifnotdata:
break
print('从%s收到信息:%s'%(self.HOST,data.decode('utf8')))
if__name__=='__main__':
client=TcpClient()
注:以上代码为Python3。
本文内容总结:
原文链接:https://www.cnblogs.com/GarfieldTom/archive/2012/12/16/2820143.html