本文實(shí)例講述了python網(wǎng)絡(luò)編程,分享給大家供大家參考。
具體方法如下:
服務(wù)端代碼如下:
from SocketServer import(TCPServer as TCP, StreamRequestHandler as SRH) from time import ctime HOST = '' PORT = 21567 ADDR = (HOST, PORT) class MyRequestHandle(SRH): def handle(self): print 'connecting from ..', self.client_address self.wfile.write("[%s]:%s" % (ctime(),self.rfile.readline()) ) tcp_Server = TCP(ADDR,MyRequestHandle) print 'WAITING connecting...' tcp_Server.serve_forever()
客戶端代碼如下:
from socket import * HOST = 'localhost' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) while True: tcpCliSock = socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) data = raw_input('>>>') if not data: break tcpCliSock.send("%s/r/n" % data) data = tcpCliSock.recv(BUFSIZE) if not data: break print data.strip() tcpCliSock.close()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選