博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python SocketServer 网络服务器的框架一:基本知识
阅读量:7082 次
发布时间:2019-06-28

本文共 4058 字,大约阅读时间需要 13 分钟。

  hot3.png

原文来自

一、简介

Python的SocketServer模块简化了编写网络服务器,他有四个基本的服务器类:「1、TCPServer2、UDPServer 3、UnixStreamServer 4、UnixDatagramServer」

1、TCPServer使用的互联网TCP协议,它提供连续的客户端和服务器之间的数据流。

2、UDPServer使用离散的数据包的信息,可以不按顺序到达

3、UnixStreamServer和UnixDatagramServer 是不常使用的,这两个类是很相似的

二、基本知识

    通常这四个类在处理请求时必须完成整个过程后才能处理下一个请求。(如果每个请求的处理过程需要很长的时间才能完成或者它传回大量数据到客户端时非常缓慢时,另一个请求只能等待)。要解决这个问题可以创建一个单独的进程或线程来处理每个请求。 ForkingMixIn和ThreadingMixIn混合类可用于支持异步操作。

    创建一个服务器需要几个步骤:

    1、首先你要创建一个请求处理程序的类,调用SocketServer类中的BaseRequestHandler子类,并重写它的handle()方法,这个方法会处理传入的请求
    2、你必须实例化一个服务器类,它通过服务器地址和请求来处理程序类。

    3、用handle_request() or serve_forever()方法来处理一个或多个请求。

三、继续

   

为防止线程在运行中突然挂掉了,所以在
继承ThreadingMixIn过程时
必须将
ThreadingMixIn类的属性
定义为
守护线程,这样服务器就知道是否应该等特
线程
或终止服务器,所以要明确设置。如果你想线程都是执行完自主关闭的(默认是False)。这就意味着Python 不会退出,直到所有的线程
ThreadingMixIn都结束。

四、注意

    在Python3中,本模块为socketserver模块。在Python 2.x中,本模块为SocketServer模块。所以在用import导入时,要分情况导入,否则会报错。导入的代码如下:

try:    import socketserver      #Python 3except ImportError:    import SocketServer      #Python 2

五、

+ ------------ +BaseServer |+ ------------ +      |      v+ ----------- + + ------------------ +| TCPSERVER | ------- | UnixStreamServer |+ ----------- + + ------------------ +      |      v+ ----------- + + -------------------- +| UDPServer | -------> | UnixDatagramServer |+ ----------- + + -------------------- +

Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer — the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both Unix server classes.
Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows:

class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass

The mix-in class must come first, since it overrides a method defined in UDPServer. Setting the various attributes also change the behavior of the underlying server mechanism.

To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the handler subclasses StreamRequestHandler or DatagramRequestHandler.

Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by different requests, since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child. In this case, you can use a threading server, but you will probably have to use locks to protect the integrity of the shared data.

On the other hand, if you are building an HTTP server where all data is stored externally (for instance, in the file system), a synchronous class will essentially render the service “deaf” while one request is being handled – which may be for a very long time if a client is slow to receive all the data it has requested. Here a threading or forking server is appropriate.

In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method.

Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork() (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use select() to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). See asyncore for another way to manage this.

转载于:https://my.oschina.net/golang/blog/82578

你可能感兴趣的文章
[翻译] Autofac 中注册的概念
查看>>
网上购物系统(Task102)——登录控件的使用(登录控件的基本配置)
查看>>
项目添加大量js文件时关闭Eclipse校验机制
查看>>
AC日记——线段树练习4 codevs 4919
查看>>
python之路--初识面向对象
查看>>
云与移动有何共同点?
查看>>
彩票软件3)wpf界面布局
查看>>
【转载】Spring Cloud全家桶主要组件及简要介绍
查看>>
Go解析写死的json
查看>>
数据库之mac上mysql root密码忘记或权限错误的解决办法
查看>>
sql变量、分页、存储过程、触发器
查看>>
zabbix server3.4 使用mailx配置邮件报警
查看>>
《软件工程》前期工作总结
查看>>
Python知识点记录四(JSON)
查看>>
DDCTF 2018线上赛writeup
查看>>
EDK2与EDK2工具链关系图
查看>>
bzoj千题计划133:bzoj3130: [Sdoi2013]费用流
查看>>
知识点7: 跨域问题
查看>>
正弦函数如何变成声音
查看>>
软工作业(一):WC.exe
查看>>