当前位置: 首页 > news >正文

阿里云虚拟主机怎么建立网站/百度一下就知道官网

阿里云虚拟主机怎么建立网站,百度一下就知道官网,益保网做推广网站吗?,wordpress文件在哪一、概述 之前上一篇写的 day8-socketserver使用 讲解了socketsever如何使用,但是在最后 简单代码实现 里面并没有实现多并发的效果,这个就郁闷了,其实不然,其实我们需要用多线程或者多线程的模块来实现 友情提示:客户…

一、概述

  之前上一篇写的 day8-socketserver使用 讲解了socketsever如何使用,但是在最后 简单代码实现 里面并没有实现多并发的效果,这个就郁闷了,其实不然,其实我们需要用多线程或者多线程的模块来实现

   友情提示:客户端代码就不用写了,这边主要写服务端的代码。

二、多用户并发

2.1、多线程

说明:主要在实例化TCPServer时,采用ThreadingTCPServer这种多线程方式

import socketserverclass MyTCPHandler(socketserver.BaseRequestHandler):"""The request handler class for our server.It is instantiated once per connection to the server, and mustoverride the handle() method to implement communication to theclient."""def handle(self):# self.request is the TCP socket connected to the clientwhile True:try:self.data = self.request.recv(1024)print("{0} write:".format(self.client_address[0]))print(self.data)self.request.send(self.data.upper())except ConnectionResetError as e:print("error:",e)breakif __name__ == "__main__":HOST,PORT = "localhost",9999server = socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler)  #采用ThreadingTCPServer多线程方式实例化server.serve_forever()server.server_close()

注:ThreadingTCPServer表示服务器每收到客户端一个请求,服务器就会开启一个线程,开启一个线程跟这个链接交互,这个新的线程就是独立的线程,如果你有10个线程,代表可以干10件事。

2.2、多进程

说明:主要在实例化TCPServer时,采用ForkingTCPServer这种多线程方式,但是这种方式在windows上不好使,需要在Linux上去执行,因为windows和linux处理多进程方式不一样。

import socketserverclass MyTCPHandler(socketserver.BaseRequestHandler):"""The request handler class for our server.It is instantiated once per connection to the server, and mustoverride the handle() method to implement communication to theclient."""def handle(self):# self.request is the TCP socket connected to the clientwhile True:try:self.data = self.request.recv(1024)print("{0} write:".format(self.client_address[0]))print(self.data)self.request.send(self.data.upper())except ConnectionResetError as e:print("error:",e)breakif __name__ == "__main__":HOST,PORT = "localhost",9999server = socketserver.ForkingTCPServer((HOST,PORT),MyTCPHandler) #采用ForkingTCPServer实现多进程server.serve_forever()server.server_close()

小结:让你的socketserver并发起来, 必须选择使用以下一个多并发的类

class socketserver.ForkingTCPServer

class socketserver.ForkingUDPServer

class socketserver.ThreadingTCPServer

class socketserver.ThreadingUDPServer

so 只需要把下面这句:

server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

 换成下面这个,就可以多并发了,这样,客户端每连进一个来,服务器端就会分配一个新的线程来处理这个客户端的请求

 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)

三、socketserver.BaseServer

class socketserver.BaseServer(server_addressRequestHandlerClass) 主要有以下方法:

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)
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()
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
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
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.
socketserver.BaseServer

 

转载于:https://www.cnblogs.com/zhangqigao/articles/7211569.html

http://www.lbrq.cn/news/1539811.html

相关文章:

  • word超链接网站怎么做/上海网络推广需要多少
  • 专门做lolh的网站/谷歌seo综合查询
  • 日照网站优化/最新国际新闻10条
  • 学做沪江网站要多久/百度账号怎么注册
  • 南安网站建设/江北seo
  • 网站开发亿码酷技术/百度公司在哪里
  • 成都住房和城乡建设厅网站首页/最佳搜索引擎磁力王
  • 洛阳万悦网站建设/网站优化内容
  • 武昌网站建设价格多少/无锡整站百度快照优化
  • 深圳网站设计公司费用/seo综合查询网站源码
  • 南昌网站优化/今日新闻10条简短
  • 广元建设工程网站/搜索引擎优化举例说明
  • 网站开发后端 书/软文营销推广
  • 深圳品牌网站制作公司/西安企业做网站
  • 买域名自己做网站/搜索引擎公司排名
  • 企业官网网站建设/网站优化推广
  • 定制网站的优势/竞价sem托管
  • 网上购物网站建设/seo如何优化图片
  • 免费网站分析seo报告是坑吗/视频外链工具
  • 加强信息管理 维护网站建设/太原自动seo
  • 厦门市建设工程安全质量协会网站/如何搜索关键词
  • 电脑制作网站用哪个软件/百度做网站需要多少钱
  • 邯郸网站建设哪能做/google google
  • 武汉做营销型网站建设/seo推广专员招聘
  • 做效果图兼职的网站有哪些/深圳网络推广外包
  • 做的网站一定要收录么/18款禁用看奶app入口
  • 大学英语作文网站/长沙网络营销外包哪家好
  • 网站标准字体样/百度seo泛解析代发排名
  • 如何查看一个网站的所有二级域名/seminar是什么意思
  • 网站会员和discuz会员同步/网络推广员
  • C++ MFC/BCG编程:文件对话框(CFileDialog、CFolderPickerDialog)
  • Git Commit 提交信息标准格式
  • 学习嵌入式的第二十一天——数据结构——链表
  • LeetCode 100 -- Day2
  • 如何用给各种IDE配置R语言环境
  • 可靠性测试:软件稳定性的守护者