Skip to content

Instantly share code, notes, and snippets.

@kshitij10496
Last active November 17, 2023 15:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshitij10496/252acde08482cb10201a1218dbd2050d to your computer and use it in GitHub Desktop.
Save kshitij10496/252acde08482cb10201a1218dbd2050d to your computer and use it in GitHub Desktop.
Setting up a Simple HTTP Server and communicating with it

The Python standard library provides a module called SimpleHTTPServer.py which can be used to setup a simple server on your local device.

Here is a brief description of how to have fun with it:

Step 01: Spin up the server

As simple as:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

The setup server we have setup will listen to all the requests through the localhost or 127.0.0.1 on the local system thorugh the port 8000. This server looks for an index.html file in the current working directory. If such a file does not, then it serves the file system i.e all the files and directories in the current path hierarchy.

However, this is of limited use as accessing files on the same system through the browser is no fun and of no practical utility. What we are interested in is serving the desired files on our local network, isn't it ? Sharing cool stuff with friends before deploying or sharing with the world.

Step 02: Locate your private IP address

Assuming you are working behind a private network i.e connected to internet through LAN, WiFi or a proxy server (in companies), you can share your data with systems connected in the same network by sharing your private IP address with the intended person.

For Linux/MacOSX(or *NIX systems):

$ ifconfig

For Windows:

$ ipconfig

There are a few hacky shell scripts to help you with this. However, these hacks are susceptible to change in OS internals. For example, on MacOSX, en0 has been set as the name for the default device for wireless connections in favour of the earlier en1. Hence, I am in favour of manually sorting through the list of active connections.

Note:

- `127.0.0.1` is the address of your localhost which is used for tasks involving your machine only.
- Your private IP address is a  IPV4 number between:
     - 10.0.0.0 - 10.255.255.255 
     - 172.16.0.0 - 172.31.255.255 
     - 192.168.0.0 - 192.168.255.255

Step 03: Convey your private IP address to the device you would like to share your files with.

On the other device, open up a browser and connect with http://<your_private_ip_address>:8000. Viola ! You will be greeted with either the index.html or the file directory.

Step 04: Shut Down the server.

Press Ctrl + C to shut down the server on the host machine and thus close the communication between the devices.

Happy networking !

@MLGHerobrine
Copy link

MLGHerobrine commented Sep 24, 2021

Is there a way to hook this up between networks using my public IP address? I tried inputting it into my code where I usually put my private IP. Using http.server.
Code:

from http import server
from http.server import BaseHTTPRequestHandler
class Serv(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
server.HTTPServer(('ip', 80), Serv).serve_forever() #ip only works with my private ip, but I want it to work with my public and communicate between two networks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment