圆月山庄资源网 Design By www.vgjia.com

本文实例讲述了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程序设计有所帮助。

标签:
python,网络,编程

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com