python网络编程实例简析
程序员文章站
2023-11-16 12:04:10
本文实例讲述了python网络编程,分享给大家供大家参考。
具体方法如下:
服务端代码如下:
from socketserver import(tcpserv...
本文实例讲述了python网络编程,分享给大家供大家参考。
具体方法如下:
服务端代码如下:
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()
希望本文所述对大家的python程序设计有所帮助。