Home >Backend Development >Python Tutorial >用Python实现一个简单的多线程TCP服务器的教程

用Python实现一个简单的多线程TCP服务器的教程

WBOY
WBOYOriginal
2016-06-06 11:16:021458browse

 最近看《python核心编程》,书中实现了一个简单的1对1的TCPserver,但是在实际使用中1对1的形势明显是不行的,所以研究了一下如何在server端通过启动不同的线程(进程)来实现每个链接一个线程。

其实python在类的设计上已经考虑到了这一方面的需求,我们只要在自己的server上继承一下SocketServer.BaseRequestHandler就可以了。
server端代码如下:

#!/usr/bin/env python 
import SocketServer 
from time import ctime 
HOST = '' 
PORT = 21567 
ADDR = (HOST, PORT) 
class MyRequestHandler(SocketServer.BaseRequestHandler): 
  def handle(self): 
    print '...connected from:', self.client_address 
    while True: 
      self.request.sendall('[%s] %s' % (ctime(),self.request.recv(1024))) 
tcpServ = SocketServer.ThreadingTCPServer(ADDR, MyRequestHandler) 
print 'waiting for connection...' 
tcpServ.serve_forever()

客户端代码如下(基本和书中一模一样,只是把循环中的关闭链接注释掉了):

#!/usr/bin/env python 
from socket import * 
HOST = 'localhost' 
PORT = 21567 
BUFSIZ = 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(BUFSIZ) 
  if not data: 
    break 
  print data.strip() 
#tcpCliSock.close()

从客户端的代码可以看出,每次输入都会建立一次新的请求。
测试一下,启动server和client之后,在client中输入测试: 

201555114612673.png (434×234)

201555114637991.png (438×202)

Statement:
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