当前位置:小鱼儿玄机二站 > 编程应用 > SocketServer的tcp和udp互联网编制程序示例,udp网络

SocketServer的tcp和udp互联网编制程序示例,udp网络

文章作者:编程应用 上传时间:2019-09-04

python: udp互连网编制程序

python中利用socket模块编写的udp公约的c/s架构的互连网程序:

#!/usr/bin/env python

#server

from socket import *

ss = socket(AF_INET, SOCK_DGRAM)
ss.bind((HOST, PORT))

while True:
data, caddr = ss.recvfrom(BUFSIZE)
if not data:
break
print "I am server, connect from %s, recved: %s" % (caddr, data)
ss.sendto("i am server",caddr)
ss.close()

########################################################

#!/usr/bin/env python
#client

from socket import *

cs = socket(AF_INET, SOCK_DGRAM)

while True:
cs.sendto("I am client", (HOST, PORT))
data, saddr = cs.recvfrom(BUFSIZE)
if not data:
break
print "I am client, connect from %s, recved: %s" % (saddr, data)
cs.close()

udp网络编制程序python中动用socket模块编写的udp公约的c/s架构的网络程序: #!/usr/bin/env python #server from socket import * ss = socket(AF_INET, SOCK_D...

python: SocketServer的tcp和udp互连网编制程序示例

运用SocketServer模块(在python3中是socketserver)编写的tcp互联网程序示例:

 

#!/usr/bin/env python

#server

from SocketServer import TCPServer as TCP, StreamRequestHandler as SRH

class MyRequestHandler(SRH):
def handle(self):
print I am server and recved: %s from %s % (self.rfile.readline().strip(), self.client_address)
self.wfile.write(I am server)

tcpServ = TCP((HOST, PORT), MyRequestHandler)
tcpServ.serve_forever()

 

################################################

#!/usr/bin/env python

#client

from socket import *

while True:
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect((HOST,PORT))
tcpCliSock.send(I am client )
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
print I am client and recved: , data
tcpCliSock.close()

 

 

#################################################################

使用SocketServer模块编制程序的udp网络程序示例:

 

#!/usr/bin/env python

#server

from SocketServer import DatagramRequestHandler as DRH, UDPServer as UDP

class MyRequestHandler(DRH):
def handle(self):
print I am server and recved: %s from %s % (self.rfile.readline(), self.client_address)
self.wfile.write(I am server)
ss = UDP(('localhost', 6543), MyRequestHandler)
ss.serve_forever()

 

 

##############################################################

#!/usr/bin/env python

#client
www.Bkjia.com
from socket import *

while True:
cs = socket(AF_INET, SOCK_DGRAM)
cs.sendto(I am client, (HOST, PORT))
data, saddr = cs.recvfrom(BUFSIZE)
if not data:
break
print 'I am client and recved: %s from %s' % (data, saddr)
cs.close()

 

SocketServer的tcp和udp网络编制程序示例 使用SocketServer模块(在python3中是socketserver)编写的tcp互联网程序示例: #!/usr/bin/env python #server from...

本文由小鱼儿玄机二站发布于编程应用,转载请注明出处:SocketServer的tcp和udp互联网编制程序示例,udp网络

关键词: