Welcome, guest | Sign In | My Account | Store | Cart

This is very very simple HTTP web server.

Python, 32 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Very Very Simple Web Server
# This can be used to demonstrate how HTTP works!!
# TO DO
#   create a simple html file
#   path = "C:/index.html"
#   open browser in address bar
#   http://127.0.0.1:50007/index.html
#
from socket import *
HOST = '127.0.0.1'                 # Symbolic name meaning the local host
PORT = 50007              # Arbitrary non-privileged port
s = socket(AF_INET,SOCK_STREAM)
s.bind((HOST, PORT))
#format of response message(DO NOT ALTER IF YOU DONT KNOW WHAT U R DOING)
str ='''HTTP/1.0 200 OK
Connection: close
Content-Length: 1
Content-Type: text/html

'''
s.listen(1)
while 1:
    conn, addr = s.accept()
    print 'Connected by', addr
    data = conn.recv(1024)
    if not data: break
    file = open(data[4:data[4:].find(' ')+4]) # extracts filename from request
    str1 = file.read()
    file.close()
    data = str +str1
    conn.send(data)
    conn.close()

3 comments

TheMachineCharmer (author) 13 years, 1 month ago  # | flag

You can run it and by observing packets you can LEARN how HTTP works. It is the basic logic on which all the popular web servers(e.g. Apache etc) work. I think it is good starting point for network newbies.

Alexander Semenov 13 years, 1 month ago  # | flag

NEVER run something like this code. It is trojan proggie. Everybody can steel all of your information after you run it.

Michal Niklas 13 years, 1 month ago  # | flag

Alexander is right. If you want to run it change HOST from '' to '127.0.0.1' and this server will accept connections from local machine only.

Created by TheMachineCharmer on Sun, 26 Oct 2008 (MIT)
Python recipes (4591)
TheMachineCharmer's recipes (4)

Required Modules

Other Information and Tasks