Before starting the new content, we will first resolve the socket error 98:address already in use problem
It is easy to notice that the port is occupied and there are many possibilities, such as shutting down the server you are hooking up with and then opening it again, and you will find this error.
This appears to be due to the nature of the TCP itself, the port is not released
We can use the following command to view the port usage and try to handle it
KILL-9 [process id] kills the process
lsof-i:[Port number] See which program the ports belong to
Netstrat TLN | grep [port number] View port usage
Well ... Online many ways to change the socket so that it can be reused or restart quickly, always feel for a server source code to add these inappropriate (in fact, I will not)
Then the Linux Shutdown Firewall command
1) Permanent, no recovery after restart: Chkconfig iptables on off: chkconfig iptables off
2) immediate effect, restart after recovery on: Service iptables start off: Service iptables stop
The issue to be discussed below is the Public Gateway Interface (CGI) mechanism, which provides a standard way for Web servers to run external programs
For example, I want to run a Python applet that shows time.
From datetime import Datetimeprint "'
Here's how it's handled.
Import sys, OS, basehttpserver#------------------------------------------------------------------------------- Class Serverexception (Exception): ' For internal error reporting. ' pass#-------------------------------------------------------------------------------class Case_no_file (object): ' File or directory does not exist. ' def test (self, handler): Return not Os.path.exists (handler.full_path) def act (self, Handler): Raise Serve Rexception ("' {0} ' not found". Format (Handler.path)) #------------------------------------------------------------- ------------------class Case_cgi_file (object): "Something runnable." def test (self, handler): Return Os.path.isfile (Handler.full_path) and Handler.full_path.endswith ('. P Y ') def act (self, Handler): handler.run_cgi (handler.full_path) #------------------------------------------------- ------------------------------class Case_existing_file (object): ' File exists. ' def Test (sElf, Handler): Return Os.path.isfile (handler.full_path) def act (self, Handler): Handler.handle_file (Handl Er.full_path) #-------------------------------------------------------------------------------class Case_ Directory_index_file (object): ' Serve index.html page for a directory. ' def index_path (self, handler): Return Os.path.join (Handler.full_path, ' index.html ') def test (self, handler): Return Os.path.isdir (Handler.full_path) and Os.path.isfile (Self.index_path (handler)) Def Act (self, H Andler): Handler.handle_file (Self.index_path (handler)) #-------------------------------------------------------- -----------------------class Case_directory_no_index_file (object): ' Serve listing for a directory without an index.ht ML page. " def index_path (self, handler): Return Os.path.join (Handler.full_path, ' index.html ') def test (self, handler): Return Os.path.isdir (Handler.full_path) and not OS.PATh.isfile (Self.index_path (handler)) Def act (self, Handler): Handler.list_dir (handler.full_path) #---------------- ---------------------------------------------------------------class Case_always_fail (object): ' Base case if Nothing else worked. " def test (self, handler): Return True def act (self, Handler): Raise Serverexception ("Unknown object ' {0} '" . Format (Handler.path) #-------------------------------------------------------------------------------class RequestHandler (Basehttpserver.basehttprequesthandler): "If The requested path maps to a file, this file is served . If Anything goes wrong, an error page is constructed. "' Cases = [Case_no_file (), Case_cgi_file (), Case_existing_file (), Case_directory_ Index_file (), Case_directory_no_index_file (), Case_always_fail ()] # How to display an error. Error_page = "" "As you can see from the run_cgi, this is the way to put the. py on the server to run. Obviously brings up some problems. For example, the server has a program that will generate a dead loop, when the client knows that there is a program, the client can be forced to run it through this CGI, the equivalent of our server was attacked
Our approach is to:
1. Run the program in a child process
2. Capture all of the child processes sent to standard output
3. Return to the client that initiated the request
The full CGI protocol is richer than this, which allows parameters to be present in the URL, and the server passes them to the running program (which seems to be the architecture of the search engine Wow, adding parameters to the URL and then dropping it into the engine and then returning an HTML),
However, this does not affect the overall architecture of the system.
So our source code is as follows:
Import sys, OS, basehttpserver#------------------------------------------------------------------------------- Class Serverexception (Exception): ' For internal error reporting. ' pass#-------------------------------------------------------------------------------class Base_case (object): " Parent for case handlers. " def handle_file (self, Handler, Full_path): Try:with open (Full_path, ' RB ') as Reader:con Tent = Reader.read () handler.send_content (content) except IOError as Msg:msg = "' {0} ' cannot Be read: {1} '. Format (Full_path, msg) Handler.handle_error (msg) def index_path (self, handler): Return Os.path.join (Handler.full_path, ' index.html ') def test (self, handler): Assert False, ' not implemented. ' Def act (self, handler): Assert False, ' not implemented. ' #-------------------------------------------------------------------------------class Case_no_file (base_case): " FilE or directory does not exist. " def test (self, handler): Return not Os.path.exists (handler.full_path) def act (self, Handler): Raise Serve Rexception ("' {0} ' not found". Format (Handler.path)) #------------------------------------------------------------- ------------------class Case_cgi_file (base_case): "Something runnable." def run_cgi (self, handler): cmd = "python" + handler.full_path Child_stdin, child_stdout = os.popen2 (cmd) Child_stdin.close () data = Child_stdout.read () child_stdout.close () handler.send_content (data) def test (self, handler): Return Os.path.isfile (Handler.full_path) and Handler.full_path.endswith ( '. Py ') def act (self, Handler): self.run_cgi (handler) #----------------------------------------------------------- --------------------class Case_existing_file (base_case): ' File exists. ' def test (self, handler): Return Os.path.isfile (Handler.full_path) dEF Act (Self, Handler): Self.handle_file (Handler, Handler.full_path) #---------------------------------------------- ---------------------------------class Case_directory_index_file (base_case): "Serve index.html page for a directory .‘‘‘ def test (self, handler): Return Os.path.isdir (Handler.full_path) and Os.path.isfile (Self.index_path ( handler) def act (self, Handler): Self.handle_file (Handler, Self.index_path (handler)) #-------------------------- -----------------------------------------------------class Case_directory_no_index_file (base_case): "Serve Listing for a directory without an index.html page. " # How to display a directory listing. Listing_page = ' Create a parent class for all of our events, and when the method is shared between multiple processors, move them into the parent class, and then reload our method separately.
Python Web server Learning notes (iv)