User Tools

Site Tools


programming:python:py-prefork-server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
programming:python:py-prefork-server [2015/01/03 05:41] – [BaseChild] jayprogramming:python:py-prefork-server [2015/01/03 05:50] – [The __init__ Signature] jay
Line 77: Line 77:
  
 ^ Name ^ Type ^ Description ^ ^ Name ^ Type ^ Description ^
-| self.proto | str | This will be either "tcp" or "udp"+| self.protocol | str | This will be either "tcp" or "udp"
-| self.reqsHandled | int | This is the number of requests this child has handled |+| self.requests_handled | int | This is the number of requests this child has handled |
 | self.conn | socket object or str | The socket object if this is a tcp server, otherwise this will be the actual payload of the udp packet | | self.conn | socket object or str | The socket object if this is a tcp server, otherwise this will be the actual payload of the udp packet |
-| self.addr | tuple(str , int) | An address tuple containing (ip , port) |+| self.address | tuple(str , int) | An address tuple containing (ip , port) |
 | self.closed | boolean | A boolean, mainly for internal use, which says whether this child has been set to be closed | | self.closed | boolean | A boolean, mainly for internal use, which says whether this child has been set to be closed |
 | self.error | str | A string error message, if set | | self.error | str | A string error message, if set |
Line 86: Line 86:
 === Hooks === === Hooks ===
 == initialize(self) == == initialize(self) ==
-Rather than reimplementing __init__, which you can do instead, you can just override this and setup variables and such that you need to set up.  This is the recommended approach.  Note that this is only called once, when the class is initialized.  This is **NOT** called on each new connection.  Use ''postAccept()'' for that purpose.+Rather than reimplementing __init__, which you can do instead, you can just override this and setup variables and such that you need to set up.  This is the recommended approach.  Note that this is only called once, when the class is initialized.  This is **NOT** called on each new connection.  Use ''post_accept()'' for that purpose.
  
-== postAccept(self) == +== post_accept(self) == 
-''self.conn'' and ''self.addr'' are set before this is called, as a new connection has been established.  You can make any modifications/setup before ''processRequest()'' is called.+''self.conn'' and ''self.address'' are set before this is called, as a new connection has been established.  You can make any modifications/setup before ''process_request()'' is called.
  
-== allowDeny(self) == +== allow_deny(self) == 
-You can use this hook to refuse the connection based on the ''self.conn'' and ''self.addr'' variables that have been set.  Return ''True'' (default) here to accept the connection and ''False'' to deny it and close the connection.  This is especially useful for IP based filtering.  See ''requestDenied()'' for sending messages back to the client before the socket is closed.  Note that this is the only hook where your return value matters.+You can use this hook to refuse the connection based on the ''self.conn'' and ''self.address'' variables that have been set.  Return ''True'' (default) here to accept the connection and ''False'' to deny it and close the connection.  This is especially useful for IP based filtering.  See ''request_denied()'' for sending messages back to the client before the socket is closed.  Note that this is the only hook where your return value matters.
  
-== requestDenied(self) == +== request_denied(self) == 
-If you deny the connection in allowDeny(), you can send a message using this callback before the connection is closed.+If you deny the connection in allow_deny(), you can send a message using this callback before the connection is closed.
  
-== processRequest(self) ==+== process_request(self) ==
 This is where you are processing the actual request.  You should use your self.conn socket to send and receive data from your client in this hook. This is where you are processing the actual request.  You should use your self.conn socket to send and receive data from your client in this hook.
  
-Remember, if this is a udp server, self.conn will be a string with the actual packet payload.  If you have a udp server, and you wish to respond, you can create a new udp connection back to the sender.+Remember, if this is a udp server, self.conn will be a string with the actual packet payload.  If you have a udp server, and you wish to respond, you can use the resp_to() method built into BaseChild.  It accepts the message responds to the current udp client.
  
-== postProcessRequest(self) ==+<source python> 
 +def process_request(self): 
 +    data self.conn 
 +    self.resp_to('Received data: %s\n' % data) 
 +</source> 
 + 
 +== post_process_request(self) ==
 This is called after the connection is closed.  You can perform any maintenance/cleanup or post connection processing here. This is called after the connection is closed.  You can perform any maintenance/cleanup or post connection processing here.
  
Line 117: Line 123:
 === The __init__ Signature === === The __init__ Signature ===
 <code python> <code python>
-def __init__(self , childClass maxServers=20 , minServers=5 , +def __init__(self , child_class max_servers=20 , min_servers=5 , 
-        minSpareServers=2 , maxSpareServers=10 , maxRequests=0 , +        min_spare_servers=2 , max_spare_servers=10 , max_requests=0 , 
-        bindIp='127.0.0.1' , port=10000 , proto='tcp' , listen=5):+        bind_ip='127.0.0.1' , port=10000 , protocol='tcp' , listen=5 
 +        reuse_port=False):
 </code> </code>
 Here is short description of each of those variables. Here is short description of each of those variables.
 ^ Name ^ Type ^ Description ^ ^ Name ^ Type ^ Description ^
-childClass | BaseChild | An implentation of BaseChild to define the child processes | +child_class | BaseChild | An implentation of BaseChild to define the child processes | 
-maxServers | int | Maximum number of children that can exist at any time | +max_cervers | int | Maximum number of children that can exist at any time | 
-minServers | int | Minimum number of children to have | +min_servers | int | Minimum number of children to have | 
-minSpareServers | int | Minimum number of spare children to have | +min_spare_servers | int | Minimum number of spare children to have | 
-maxSpareServers | int | Maximum number of spare children to have | +max_spare_servers | int | Maximum number of spare children to have | 
-maxRequests | int | Maximum number of requests each child should handle.  Zero is unlimited and the default. | +max_requests | int | Maximum number of requests each child should handle.  Zero is unlimited and the default. | 
-bindIp | str | The IP address to bind to |+bind_ip | str | The IP address to bind to |
 | port | int | The port number to bind to | | port | int | The port number to bind to |
-proto | str | The protocol to use (tcp or udp) |+protocol | str | The protocol to use (tcp or udp) |
 | listen | int | TCP listen queue backlog | | listen | int | TCP listen queue backlog |
 +| reuse_port | bool | If set, and available, SO_REUSEPORT will be used to bind the socket in the child |
 For anyone who has set up an Apache prefork server (or used Perl's ''Net::Server::PreFork''), this should look pretty familiar. For anyone who has set up an Apache prefork server (or used Perl's ''Net::Server::PreFork''), this should look pretty familiar.
  
programming/python/py-prefork-server.txt · Last modified: 2023/11/10 20:06 by jay