Pages

Saturday, January 14, 2012

Starting a simple HTTPServer in python

Python comes with its own web server included in the http.server library module. 

To start a web server in python:

from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer((' ',port), CGIHTTPRequestHandler)
httpd.server_forever()

After starting the server when you run localhost:8080 on your browser it shows you the current directory from where the server was started. That can be changed by importing the os module.

Accessing the server on the network:

After starting the server, create a wireless network. When another computer connects to the network, the server from the first computer can be accessed using the IP address and port number. To find the IP address. 

Run the following command to find the ip address:

ifconfig

After finding the IP address of the machine where the server is hosted, using the IP address and the port number the server can be accessed on the other machine.

[IP Address]:8080


No comments:

Post a Comment