socket并不能多并发,只能支持一个用户,socketserver 简化了编写网络服务程序的任务,socketserver是socket的在封装。socketserver在python2中为SocketServer,在python3种取消了首字母大写,改名为socketserver。socketserver中包含了两种类,一种为服务类(server class),一种为请求处理类(request handle class)。前者提供了许多方法:像绑定,监听,运行…… (也就是建立连接的过程) 后者则专注于如何处理用户所发送的数据(也就是事务逻辑)。一般情况下,所有的服务,都是先建立连接,也就是建立一个服务类的实例,然后开始处理用户请求,也就是建立一个请求处理类的实例。
 
socketserver一共有这么几种类型:
 
1. class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)
2. class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)
3. class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)
4. class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activate=True)
 
继承关系图:
+------------+
| BaseServer | 

+------------+
      |
      v
+-----------+        +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+        +------------------+
      |
      v
+-----------+        +--------------------+
| UDPServer |------->| UnixDatagramServer |

+-----------+        +--------------------+
 
如何创建一个socketserver :
 
  1. First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; this method will process incoming requests.   
  2. Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
  3. Then call the handle_request() orserve_forever() method of the server object to process one or many requests.
  4. Finally, call server_close() to close the socket.
 
  1. 创建一个请求处理的类,并且这个类要继承 BaseRequestHandlerclass ,并且还要重写父类里handle()方法;
  2. 你必须实例化 TCPServer,并且传递server IP和你上面创建的请求处理类,给这个TCPServer;
  3. server.handle_requese()#只处理一个请求,server.server_forever()处理多个一个请求,永远执行
  4. 关闭连接server_close()
 
socketserver例子:
 
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler): #服务类,监听绑定等等
"""
The request handler class for our server. It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
""" def handle(self): #请求处理类,所有请求的交互都是在handle里执行的
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()#每一个请求都会实例化MyTCPHandler(socketserver.BaseRequestHandler):
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())#sendall是重复调用send. if __name__ == "__main__":
HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever() server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) #线程
# server = socketserver.ForkingTCPServer((HOST, PORT), MyTCPHandler) #多进程 linux适用
# server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) 单进程
 
socketserver.py源码解读(多线程如何开启后续补充)
 
def process_request(self, request, client_address):
"""Start a new thread to process the request."""
t = threading.Thread(target = self.process_request_thread, #开始一个新线程传一个实例进去
args = (request, client_address))
t.daemon = self.daemon_threads
t.start()
 
class socketserver.BaseServer(server_address, RequestHandlerClass) 主要有以下方法: 
 
class socketserver.BaseServer(server_address, RequestHandlerClass)
This is the superclass of all Server objects in the module. It defines the interface, given below, but does not implement most of the methods, which is done in subclasses. The two parameters are stored in the respective server_address and RequestHandlerClass attributes.

 
fileno()  #返回文件描述符

Return an integer file descriptor for the socket on which the server is listening. This function is most commonly passed to selectors, to allow monitoring multiple servers in the same process.

 
handle_request() #处理单个请求

Process a single request. This function calls the following methods in order: get_request(), verify_request(), and process_request(). If the user-provided handle() method of the handler class raises an exception, the server’s handle_error() method will be called. If no request is received within timeout seconds, handle_timeout() will be called and handle_request() will return.

 
serve_forever(poll_interval=0.5)  #每0.5秒检查下是否发了shutdown信号,做一些清理工作
Handle requests until an explicit(明确的) shutdown() request. Poll(检查) for shutdown every poll_interval seconds. Ignores the timeout attribute. It also calls service_actions(), which may be used by a subclass or mixin to provide actions specific to a given service. For example, the ForkingMixIn class uses service_actions() to clean up zombie child processes.

Changed in version 3.3: Added service_actions call to the serve_forever method.

 
service_actions() #python3.3里加入,

This is called in the serve_forever() loop. This method can be overridden by subclasses or mixin classes to perform actions specific to a given service, such as cleanup actions.

New in version 3.3.

shutdown() #停掉
Tell the serve_forever() loop to stop and wait until it does.

server_close()#关闭
Clean up the server. May be overridden.

 
address_family #地址蔟

The family of protocols to which the server’s socket belongs. Common examples are socket.AF_INET and socket.AF_UNIX.

 
RequestHandlerClass #请求处理类

The user-provided request handler class; an instance of this class is created for each request.

server_address#IP地址
The address on which the server is listening. The format of addresses varies depending on the protocol family; see the documentation for the socket module for details. For Internet protocols, this is a tuple containing a string giving the address, and an integer port number: ('127.0.0.1', 80), for example.

socket #
The socket object on which the server will listen for incoming requests.

The server classes support the following class variables:

 
allow_reuse_address  #准许重用地址,普通的socket重用地址

Whether the server will allow the reuse of an address. This defaults to False, and can be set in subclasses to change the policy.

 
request_queue_size  #

The size of the request queue. If it takes a long time to process a single request, any requests that arrive while the server is busy are placed into a queue, up to request_queue_size requests. Once the queue is full, further requests from clients will get a “Connection denied” error. The default value is usually 5, but this can be overridden by subclasses.

socket_type  #协议类型
The type of socket used by the server; socket.SOCK_STREAM and socket.SOCK_DGRAM are two common values.

 
timeout #超时

Timeout duration, measured in seconds, or None if no timeout is desired. If handle_request() receives no incoming requests within the timeout period, the handle_timeout() method is called.

There are various server methods that can be overridden by subclasses of base server classes like TCPServer; these methods aren’t useful to external users of the server object.

 
finish_request()  
Actually processes the request by instantiating RequestHandlerClass and calling its handle() method.
 
 
get_request()  #

Must accept a request from the socket, and return a 2-tuple containing the new socket object to be used to communicate with the client, and the client’s address.

handle_error(request, client_address) #处理错误
This function is called if the handle() method of a RequestHandlerClass instance raises an exception. The default action is to print the traceback to standard output and continue handling further requests.

handle_timeout() #
This function is called when the timeout attribute has been set to a value other than None and the timeout period has passed with no requests being received. The default action for forking servers is to collect the status of any child processes that have exited, while in threading servers this method does nothing.

 
process_request(request, client_address) #单个请求

Calls finish_request() to create an instance of the RequestHandlerClass. If desired, this function can create a new process or thread to handle the request; the ForkingMixIn and ThreadingMixIn classes do this.

server_activate()
Called by the server’s constructor to activate the server. The default behavior for a TCP server just invokes listen() on the server’s socket. May be overridden.

server_bind() #内部调用
Called by the server’s constructor to bind the socket to the desired address. May be overridden.

verify_request(request, client_address) #判断一个请求是否合法

Must return a Boolean value; if the value is True, the request will be processed, and if it’s False, the request will be denied. This function can be overridden to implement access controls for a server. The default implementation always returns True.
 
服务端和客户端代码演示:
 
Server : 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self): #所有请求的交互都是在handle里执行的,
while True:
try:
self.data = self.request.recv(1024).strip()#每一个请求都会实例化MyTCPHandler(socketserver.BaseRequestHandler):
print("{} wrote:".format(self.client_address[0]))
print(self.data)
self.request.sendall(self.data.upper())#sendall是重复调用send.
except ConnectionResetError as e:
print("err ",e)
break if __name__ == "__main__":
HOST, PORT = "localhost", 9999 #windows
#HOST, PORT = "0.0.0.0", 9999 #Linux
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) #线程
server.serve_forever() 
Client : 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
#客户端
import socket
client = socket.socket() #定义协议类型,相当于生命socket类型,同时生成socket连接对象
client.connect(('192.168.84.130',9999))
while True:
msg = input(">>>").strip()
if len(msg) ==0:continue
client.send(msg.encode("utf-8"))
data = client.recv(1024)#这里是字节1k
print("recv:>",data.decode())
client.close()

Python3中的SocketServer的更多相关文章

  1. Python3中的http.client模块

    http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块. http.client 是一个底层的 HTTP 协议客户端,被更高层的 urllib.request ...

  2. Python3中的字符串函数学习总结

    这篇文章主要介绍了Python3中的字符串函数学习总结,本文讲解了格式化类方法.查找 & 替换类方法.拆分 & 组合类方法等内容,需要的朋友可以参考下. Sequence Types ...

  3. Python3中使用PyMySQL连接Mysql

    Python3中使用PyMySQL连接Mysql 在Python2中连接Mysql数据库用的是MySQLdb,在Python3中连接Mysql数据库用的是PyMySQL,因为MySQLdb不支持Pyt ...

  4. python3 中mlpy模块安装 出现 failed with error code 1的决绝办法(其他模块也可用本方法)

    在python3 中安装其它模块时经常出现 failed with error code 1等状况,使的安装无法进行.而解决这个问题又非常麻烦. 接下来以mlpy为例,介绍一种解决此类安装问题的办法. ...

  5. python3中返回字典的键

    我在看<父与子的编程之旅>的时候,有段代码是随机画100个矩形,矩形的大小,线条的粗细,颜色都是随机的,代码如下, import pygame,sys,random from pygame ...

  6. python3中的zip

    在 python2 中zip可以将两个列表并入一个元组列表,如: a = [1,2,3,4] b = [5,6,7,8] c = zip(a,b) 结果:c [(1,5),(2,6),(3,7),(4 ...

  7. python3中输出不换行

    python2中输出默认是换行的,为了抑制换行,是这么做的: print x, 到了python3中,print变成一个函数,这种语法便行不通了.用2to3工具转换了下,变成这样了: print(x, ...

  8. Python3中的新特性(3)——代码迁移与2to3

    1.将代码移植到Python2.6 建议任何要将代码移植到Python3的用户首先将代码移植到Python2.6.Python2.6不仅与Python2.5向后兼容,而且支持Python3中的部分新特 ...

  9. Python3中的新特性(1)——新的语言特性

    1.源代码编码和标识符         Python3假定源代码使用UTF-8编码.另外,关于标识符中哪些字符是合法的规则也放宽了.特别是,标识符可以包含代码点为U+0080及以上的任意有效Unico ...

随机推荐

  1. (转)设置Sysctl.conf用以提高Linux的性能(最完整的sysctl.conf优化方案)

    Sysctl是一个允许您改变正在运行中的Linux系统的接口.它包含一些 TCP/IP 堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能.用sysctl可以读取设置超过五百 ...

  2. Java知识点整理(一)

    ArrayList和LinkedList的区别 1.ArrayList和LinkedList可想从名字分析,它们一个是Array(动态数组)的数据结构,一个是Link(链表)的数据结构,此外,它们两个 ...

  3. JVM 内部原理系列

    JVM 内部原理(一)— 概述 JVM 内部原理(二)— 基本概念之字节码 JVM 内部原理(三)— 基本概念之类文件格式 JVM 内部原理(四)— 基本概念之 JVM 结构 JVM 内部原理(五)— ...

  4. 'phantomjs.exe' executable needs to be in PATH. (selenium PhantomJS python)

    今天selenium PhantomJS python用了下,发现报错,提示我:'phantomjs.exe' executable needs to be in PATH. from seleniu ...

  5. 第193天:js---Math+Error+Number+Object总结

    一.Math 随机选取 //随机选取 function getRandom (begin,end){ return Math.floor(Math.random()*(end-begin))+begi ...

  6. jquery.fullpage 全屏滚动

    参考文档 :http://www.dowebok.com/77.html 下载地址: https://github.com/alvarotrigo/fullPage.js 1. 使用 HTML < ...

  7. ZOJ2083_Win the Game

    这个题目很有趣,有博弈知识,又有一点智商题的感觉. 题意为给你一段长为n的的线段. 两个游戏者轮流在一段长为2,未被染色的线段上涂色. 无法涂色的游戏者输. 题目有点灵活.关键在于怎么得到Sg函数值呢 ...

  8. 【bzoj1430】小猴打架 Prufer序列

    题目描述 给出 $n$ 个点,每次选择任意一条边,问这样 $n-1$ 次后得到一棵树的方案数是多少. 输入 一个整数N. 输出 一行,方案数mod 9999991. 样例输入 4 样例输出 96 题解 ...

  9. 【BZOJ1492】【NOI2007】货币兑换(动态规划,CDQ分治,Splay)

    [BZOJ1492][NOI2007]货币兑换(动态规划,CDQ分治,Splay) 题面 BZOJ 洛谷 Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券 ...

  10. NOI2013 矩阵游戏 【数论】

    题目描述 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i ...