import ossys from http server import httpservercgihttprequesthandler webdir port where your html files and cgi-bin script directory live if len(sys argv webdir sys argv[ if len(sys argv port int(sys argv[ ]print('webdir "% "port % (webdirport)command-line args else default os chdir(webdirrun in html root dir srvraddr (''portmy hostnameportnumber srvrobj httpserver(srvraddrcgihttprequesthandlersrvrobj serve_forever(serve clients till exit to start the server to run this examplessimply run this script from the directory the script' file is located inwith no command-line arguments for instancefrom dos command linec:\pp \internet\webwebserver py webdir "port on windowsyou can simply click its icon and keep the console window openor launch it from dos command prompt on unix it can be run from command line in the backgroundor in its own terminal window some platforms may also require you to have administrator privileges to run servers on reserved portssuch as the web' port if this includes your machineeither run the server with the required permissionsor run on an alternate port number (more on port numbers later in this by defaultwhile running locally this waythe script serves up html pages requested on "localhostfrom the directory it lives in or is launched fromand runs python cgi scripts from the cgi-bin subdirectory located therechange its webdir variable or pass in command-line argument to point it to different directory because of this structurein the examples distribution html files are in the same directory as the web server script and cgi scripts are located in the cgi-bin subdirectory in other wordsto visit web pages and run scriptswe'll be using urls of these formsrespectivelyboth map to the directory that contains the web server script (pp \internet\webby default againto run the examples on different server machine of your ownsimply replace the "localhostand "localhost/cgi-binparts of these addresses with your server name and directory path details (more on urls later in this with this address change the examples work the samebut requests are routed across network to the serverinstead of being routed between programs running on the same local machine the server in example - is by no means production-grade web serverbut it can be used to experiment with this book' examples and is viable as way to test your cgi server-side scripting