Home > Backend Development > Python Tutorial > python网络编程实例简析

python网络编程实例简析

WBOY
Release: 2016-06-06 11:32:46
Original
1206 people have browsed it

本文实例讲述了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() 

Copy after login

客户端代码如下:

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() 

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template