Programming Memoirs

Simple Python HTTP server using sockets

I brewed a simple Python-based http server using sockets today and I wanted to share the code, along with some comments. Sure, there is the http.server class, but why not have some fun? :-) Building a fully-fledged HTTP server is a big undertaking. I focused on building a small server, supporting only the basic functionality of the Hyper Text Transfer Protocol (namely the GET and HEAD request methods).

Some basic info

In short, HTTP is a request-response protocol. The web browser attempts to connects to the http server, HTTP server (usually) accepts the connection requests and goes into receiving mode. The client (e.g. web browser) sends one of the nine request methods to the server, along with the methods arguments. Server then processes the request and issues appropriate response. An example request looks like this:

As I have already mentioned, my server support only a GET and HEAD request method. the GET request method request specific a resource from the server. The server responds with headers and the resource body. The HEAD method is similar to GET, it differs in that the server responds only with the headers. This can be useful when a client wants to determine whether e.g. a time stamp on a resource has changed, and whether it has to be re-requested.

I designed by server class to expect a port number on which it is to listen for connection, with a default value of 80.  However you might need to run the server script in a privileged mode in order to successfully acquire this (or any other low) port from your system. Thus, in case the script fails to acquire the default or user provided port, it will attempt to acquire port 8080 as a last resort.

When the client requests a resource, the server attempts to open it from the local www/ folder. If the resource is present, server serves it, accompanied with appropriate headers (status 200 OK). If the resource is not present, the server then serves a error 404 web page, along with a slightly different header set describing the encountered problem.

Notice, that web server does parse in any way the GET arguments if they are provided in the URL (URL encoded arguments, in the form of index.html?arg1=val1&arg2=val2). It does, however, strip them from the URL, if they are present and servers the resource. Also note that this server runs only a single thread, thus serving one user at a time. The server is not meant to be fast nor secure. I must say that coding this server made for fun-filled afternoon. If I will have some free time in the future maybe I will extend it’s functionality a bit.

Example request-reply

Sample HEAD request to my server issued by Firefox

HEAD /index.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

The most important is the first line. It specifies the request method (HEAD) and it’s arguments, most notably the file which is being requested. The second lines specifies the internet host and port of the resource requested. Lack of port following the local host implies port 80. More on the following lines can be found here. Response

Sample response headers sent from the server to the client:

HTTP/1.1 200 OK
Date: Sat, 19 Mar 2011 16:42:17
Server: Simple-Python-HTTP-Server
Connection: close

Notice, that it’s only basic response, containing only some basic information — most notably the first line, confirming that the requested resource exists. For the contrast, here is a sample response header from the server after the client had requested a non-existing resource:

HTTP/1.1 404 Not Found
Date: Sat, 19 Mar 2011 16:44:53
Server: Simple-Python-HTTP-Server
Connection: close

Final HTTP server code

And now for the code. Oh, and here’s the code along with some simple html/css/jpg files for testing: python-http-server.tar.gz. Also, you might want to check out some kind of a browser plugin which allows to issue custom HTTP request methods and view the response, such as this one.

#!/usr/bin/python

import socket  # Networking support
import signal  # Signal support (server shutdown on signal receive)
import time    # Current time

class Server:
 """ Class describing a simple HTTP server objects."""

 def __init__(self, port = 80):
     """ Constructor """
     self.host = ''   # <-- works on all avaivable network interfaces
     self.port = port
     self.www_dir = 'www' # Directory where webpage files are stored

 def activate_server(self):
     """ Attempts to aquire the socket and launch the server """
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     try: # user provided in the __init__() port may be unavaivable
         print("Launching HTTP server on ", self.host, ":",self.port)
         self.socket.bind((self.host, self.port))

     except Exception as e:
         print ("Warning: Could not aquite port:",self.port,"\n")
         print ("I will try a higher port")
         # store to user provideed port locally for later (in case 8080 fails)
         user_port = self.port
         self.port = 8080

         try:
             print("Launching HTTP server on ", self.host, ":",self.port)
             self.socket.bind((self.host, self.port))

         except Exception as e:
             print("ERROR: Failed to acquire sockets for ports ", user_port, " and 8080. ")
             print("Try running the Server in a privileged user mode.")
             self.shutdown()
             import sys
             sys.exit(1)

     print ("Server successfully acquired the socket with port:", self.port)
     print ("Press Ctrl+C to shut down the server and exit.")
     self._wait_for_connections()

 def shutdown(self):
     """ Shut down the server """
     try:
         print("Shutting down the server")
         s.socket.shutdown(socket.SHUT_RDWR)

     except Exception as e:
         print("Warning: could not shut down the socket. Maybe it was already closed?",e)

 def _gen_headers(self,  code):
     """ Generates HTTP response Headers. Ommits the first line! """

     # determine response code
     h = ''
     if (code == 200):
        h = 'HTTP/1.1 200 OK\n'
     elif(code == 404):
        h = 'HTTP/1.1 404 Not Found\n'

     # write further headers
     current_date = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
     h += 'Date: ' + current_date +'\n'
     h += 'Server: Simple-Python-HTTP-Server\n'
     h += 'Connection: close\n\n'  # signal that the conection wil be closed after complting the request

     return h

 def _wait_for_connections(self):
     """ Main loop awaiting connections """
     while True:
         print ("Awaiting New connection")
         self.socket.listen(3) # maximum number of queued connections

         conn, addr = self.socket.accept()
         # conn - socket to client
         # addr - clients address

         print("Got connection from:", addr)

         data = conn.recv(1024) #receive data from client
         string = bytes.decode(data) #decode it to string

         #determine request method  (HEAD and GET are supported)
         request_method = string.split(' ')[0]
         print ("Method: ", request_method)
         print ("Request body: ", string)

         #if string[0:3] == 'GET':
         if (request_method == 'GET') | (request_method == 'HEAD'):
             #file_requested = string[4:]

             # split on space "GET /file.html" -into-> ('GET','file.html',...)
             file_requested = string.split(' ')
             file_requested = file_requested[1] # get 2nd element

             #Check for URL arguments. Disregard them
             file_requested = file_requested.split('?')[0]  # disregard anything after '?'

             if (file_requested == '/'):  # in case no file is specified by the browser
                 file_requested = '/index.html' # load index.html by default

             file_requested = self.www_dir + file_requested
             print ("Serving web page [",file_requested,"]")

             ## Load file content
             try:
                 file_handler = open(file_requested,'rb')
                 if (request_method == 'GET'):  #only read the file when GET
                     response_content = file_handler.read() # read file content
                 file_handler.close()

                 response_headers = self._gen_headers( 200)

             except Exception as e: #in case file was not found, generate 404 page
                 print ("Warning, file not found. Serving response code 404\n", e)
                 response_headers = self._gen_headers( 404)

                 if (request_method == 'GET'):
                    response_content = b"<html><body><p>Error 404: File not found</p><p>Python HTTP server</p></body></html>"

             server_response =  response_headers.encode() # return headers for GET and HEAD
             if (request_method == 'GET'):
                 server_response +=  response_content  # return additional conten for GET only

             conn.send(server_response)
             print ("Closing connection with client")
             conn.close()

         else:
             print("Unknown HTTP request method:", request_method)

def graceful_shutdown(sig, dummy):
    """ This function shuts down the server. It's triggered
    by SIGINT signal """
    s.shutdown() #shut down the server
    import sys
    sys.exit(1)

###########################################################
# shut down on ctrl+c
signal.signal(signal.SIGINT, graceful_shutdown)

print ("Starting web server")
s = Server(80)  # construct server object
s.activate_server() # aquire the socket

6,691 ResponsesLeave one →

  1. [url=https://deeshanell10.kgup.info/dWR3qIiWZIOehWo/perfectly-cut][img]https://i.ytimg.com/vi/D-CwS52LiP4/hqdefault.jpg[/img][/url]

    [url=https://deeshanell10.kgup.info/dWR3qIiWZIOehWo/perfectly-cut]PERFECTLY cut screams 2020Reaction[/url]

    Reply
  2. [url=https://nkrupskiy.lvlocal.info/problemy-sobstvennogo/gK6pzIJnb4SuaKc.html][img]https://i.ytimg.com/vi/IxChR08Ny3o/hqdefault.jpg[/img][/url]

    Проблемы собственного бизнеса в США / Работаю [url=https://nkrupskiy.lvlocal.info/problemy-sobstvennogo/gK6pzIJnb4SuaKc.html]за[/url] отзывы / Свич 1.18

    Reply
  3. [url=https://skincaretabs.com/]can you buy betnovate cream over the counter[/url] [url=https://antibiotics24.com/]cefixime 500 mg[/url] [url=https://hydroxychloroquinefda.com/]plaquenil 200mg tablets 100[/url]

    Reply
  4. [url=http://sertralinezoloft.com/]price of zoloft 50 mg[/url]

    Reply
  5. [url=https://zoloftsrtl.com/]zoloft pills[/url]

    Reply
  6. [url=http://onlineloanspot.com/]credit loans[/url]

    Reply
  7. [url=http://writingserviceintl.com/]successful college essay[/url]

    Reply
  8. [url=https://warnerbrospictures.pablack.info/godzilla-king-of-the-monsters-intimidation-now-playing-in-theaters/f6DZmtak1ba8mKw.html][img]https://i.ytimg.com/vi/MnxerBqUZgw/hqdefault.jpg[/img][/url]

    Godzilla: King of the Monsters – Intimidation – Now Playing [url=https://warnerbrospictures.pablack.info/godzilla-king-of-the-monsters-intimidation-now-playing-in-theaters/f6DZmtak1ba8mKw.html]In[/url] Theaters

    Reply
  9. [url=https://writemyessayjoe.com/]college application essay titles[/url]

    Reply
  10. [url=https://paydayloansaol.com/]loans online payday[/url]

    Reply
  11. [url=http://customwriting.us.com/]mba dissertation writing services[/url] [url=http://writemyessayjoe.com/]writing essays worksheet[/url] [url=http://homework.us.org/]homework help victorians[/url]

    Reply
  12. [url=https://erythromycintabs.com/]erythromycin otc drug[/url]

    Reply
  13. [url=http://advairmeds.com/]can you buy advair over the counter[/url]

    Reply
  14. [url=https://mavienoelle.deworld.info/qc_FZavAomZ4eY0/bff-schneekugel][img]https://i.ytimg.com/vi/pmc5E_l0ECU/hqdefault.jpg[/img][/url]

    BFF [url=https://mavienoelle.deworld.info/qc_FZavAomZ4eY0/bff-schneekugel]SCHNEEKUGEL[/url] BATTLE рџ‘Ї WER IST SCHNELLER ?MaVie Noelle Werbung

    Reply
  15. [url=https://nicky1231231.thlists.info/h-n-ngs-x-l-m-k-ney/y6Ov14mjo5OPa3w.html][img]https://i.ytimg.com/vi/ikxqPlnbZ4E/hqdefault.jpg[/img][/url]

    หนังสือเล่มเก่า – а№Ђаё™аёў аё аё±аёЄаё§аёЈаёЈаё“ [url=https://nicky1231231.thlists.info/h-n-ngs-x-l-m-k-ney/y6Ov14mjo5OPa3w.html]SODA5[/url] l TEASER l 16/06/62

    Reply
  16. [url=https://trapl69.plworld.info/l4l7oZq6ppWwmps/6ix9ine-wpad-i-ci-gle-kr-ci-lil-uzi-vert-vs-yk-osiris][img]https://i.ytimg.com/vi/gYDm4TocKh8/hqdefault.jpg[/img][/url]

    6ix9ine WPADŁ i ciągle [url=https://trapl69.plworld.info/l4l7oZq6ppWwmps/6ix9ine-wpad-i-ci-gle-kr-ci-lil-uzi-vert-vs-yk-osiris]KRĘCI😲[/url] LIL UZI VERT vs. YK OSIRIS

    Reply
  17. [url=https://antabusedis.com/]rx antabuse[/url]

    Reply
  18. [url=http://buytrental.com/]trental 400 mg 1mg[/url] [url=http://effexorx.com/]effexor 200 mg[/url] [url=http://trazodone5.com/]desyrel 100 mg tablet[/url]

    Reply
  19. [url=https://suhagrabest.com/]buy suhagra 50 mg[/url]

    Reply
  20. [url=http://silagratabs.com/]buy silagra online[/url]

    Reply
  21. [url=http://antabuze.com/]buy antabuse without prescripition[/url] [url=http://baclofengen.com/]buy baclofen in uk[/url] [url=http://buytrental.com/]trental 400 order online india[/url] [url=http://hydroxychloroquinexl.com/]how much is plaquenil[/url]

    Reply
  22. [url=https://gattu.hugets.info/zqGYoMm8zpxoo6A/new-rainbow-rodeo-wrap-is-awesome-fortnite-battle-royale.html][img]https://i.ytimg.com/vi/mqekdXhf6Ao/hqdefault.jpg[/img][/url]

    NEW RAINBOW RODEO WRAP IS AWESOME (Fortnite [url=https://gattu.hugets.info/zqGYoMm8zpxoo6A/new-rainbow-rodeo-wrap-is-awesome-fortnite-battle-royale.html]Battle[/url] Royale)

    Reply
  23. [url=https://ozrealman.thworld.info/ooCrvIvZyLBu02o/k-l-ng-ph-me-x-khn-th-ng-b-n-kl-y-p-n-p-h-m-m-h-n-khotr-h-lxn.html][img]https://i.ytimg.com/vi/lGyWZvdw5m8/hqdefault.jpg[/img][/url]

    [url=https://ozrealman.thworld.info/ooCrvIvZyLBu02o/k-l-ng-ph-me-x-khn-th-ng-b-n-kl-y-p-n-p-h-m-m-h-n-khotr-h-lxn.html]แกล้งพี่เมื่อคนทั้งบ้านกลายเป็นผีไม่มีหน้า โคตรหลอน[/url]

    Reply
  24. [url=https://augmentin500.com/]augmentin script[/url]

    Reply
  25. [url=https://seroquelrx.com/]seroquel benzodiazepine[/url]

    Reply
  26. [url=http://zoloftsrt.com/]cost of zoloft 50 mg[/url] [url=http://kamagramd.com/]female kamagra jelly[/url]

    Reply
  27. [url=https://nucappella.jpfilm.info/happy-birthday-to-me-barber-shop-ver-1r-nakapera/q5V5lbGplYDXYXo.html][img]https://i.ytimg.com/vi/zdG0Py0Iv0A/hqdefault.jpg[/img][/url]

    Happy Birthday to [url=https://nucappella.jpfilm.info/happy-birthday-to-me-barber-shop-ver-1r-nakapera/q5V5lbGplYDXYXo.html]me[/url] (Barber Shop ver.?)гЂђ1дєєг‚ўг‚«гѓљгѓ©гЂ‘

    Reply
  28. [url=https://thebestutorialsmk.slpost.info/YaPNi56gf1_TlKk/24h-na-dejtu-u-fi-i-sa-chodinom-ribom-smuvali-se.html][img]https://i.ytimg.com/vi/0lhXjiO-o0w/hqdefault.jpg[/img][/url]

    24H NA DEJTU U FIĆI SA CHODINOM [url=https://thebestutorialsmk.slpost.info/YaPNi56gf1_TlKk/24h-na-dejtu-u-fi-i-sa-chodinom-ribom-smuvali-se.html]RIBOM[/url] smuvali se

    Reply
  29. [url=https://drewgooden1.alworld.info/what-is-rick-lax-doing/lZy5r4eem5Juo7s.html][img]https://i.ytimg.com/vi/dkWKQje05mU/hqdefault.jpg[/img][/url]

    What [url=https://drewgooden1.alworld.info/what-is-rick-lax-doing/lZy5r4eem5Juo7s.html]is[/url] Rick Lax doing?

    Reply
  30. [url=https://arimidextab.com/]where can i buy arimidex online[/url]

    Reply
  31. [url=http://flagyltab.com/]purchase flagyl online[/url] [url=http://nexium365.com/]buy nexium online canada[/url] [url=http://diclofenacvlt.com/]voltaren gel generic price[/url]

    Reply
  32. [url=https://kamagramd.com/]kamagra 100mg oral jelly sildenafil[/url] [url=https://sildenafil.us.org/]cheap sildenafil from india[/url] [url=https://abilify36.com/]abilify 300 mg[/url] [url=https://bupropion2.com/]average cost of wellbutrin[/url]

    Reply
  33. [url=http://diclofenacvlt.com/]buy voltaren gel canada[/url]

    Reply
  34. [url=https://kamagramd.com/]sildenafil oral jelly kamagra[/url] [url=https://dapoxetinetab.com/]super avana price in india[/url] [url=https://robaxin365.com/]robaxin medicine[/url]

    Reply
  35. [url=https://bupropion2.com/]bupropion 90 mg[/url] [url=https://abilify36.com/]generic abilify 2015[/url] [url=https://sildalis365.com/]generic sildalis[/url]

    Reply
  36. [url=https://seroquelrx.com/]seroquel online order[/url] [url=https://suhagratab.com/]suhagra tablet[/url]

    Reply
  37. [url=https://lanoxin24.com/]generic digoxin cost[/url]

    Reply
  38. [url=http://chloroquinev.com/]melubrin[/url]

    Reply
  39. [url=http://cytotectab.com/]cytotec online uk[/url]

    Reply
  40. [url=https://shimehd.rsloft.info/otkrio-sam/l9KclaXgzJa1kX4.html][img]https://i.ytimg.com/vi/eok1lzieTZM/hqdefault.jpg[/img][/url]

    OTKRIO SAM TKO JE MISTERIOZNA OSOBA – Minecraft [url=https://shimehd.rsloft.info/otkrio-sam/l9KclaXgzJa1kX4.html]Prezivljavanje[/url]

    Reply
  41. [url=https://kralsakir.trpost.info/kral---ak--r/lMqkq5GUx53TepY.html][img]https://i.ytimg.com/vi/ahnuZca7oHc/hqdefault.jpg[/img][/url]

    KRAL [url=https://kralsakir.trpost.info/kral---ak--r/lMqkq5GUx53TepY.html]ЕћAKД°R:[/url] Gizemli MГјzik – 115. BГ–LГњM

    Reply
  42. [url=https://dscoff.azfun.info/1YDCssqki7OVoKY/volga-kgb-vse-h----davaj-po-novoj][img]https://i.ytimg.com/vi/qMaPekTQ0jA/hqdefault.jpg[/img][/url]

    Волга КГБ — все [url=https://dscoff.azfun.info/1YDCssqki7OVoKY/volga-kgb-vse-h----davaj-po-novoj]хя,[/url] давай по новой

    Reply
  43. [url=https://rahimproduction.rudesk.info/folbina-fireb/sZSssHKwxaqkja4][img]https://i.ytimg.com/vi/KbtKBL_FAYI/hqdefault.jpg[/img][/url]

    Фолбина Фиреб Кардм 500см Сухтм.Хонат Чазир [url=https://rahimproduction.rudesk.info/folbina-fireb/sZSssHKwxaqkja4]Шава[/url] Чодугар баи Шайтонит 2020

    Reply
  44. Hi, here on the forum guys advised a cool Dating site, be sure to register – you will not REGRET it [url=https://bit.ly/2RA7I5l]https://bit.ly/2RA7I5l[/url]

    Reply
  45. [url=https://effexorxs.com/]effexor 37 5 mg[/url] [url=https://flagyl911.com/]generic flagyl[/url] [url=https://furosemide3.com/]buy furosemide 100 mg online[/url] [url=https://malegraxt.com/]malegra canada[/url] [url=https://trazodone365.com/]trazodone generic[/url]

    Reply
  46. [url=https://baclofen365.com/]buy baclofen uk[/url]

    Reply
  47. [url=http://bupropionb.com/]wellbutrin sr 150 mg[/url]

    Reply
  48. [url=https://inderaltab.com/]inderal 10mg tab[/url] [url=https://trazodone365.com/]trazodone 250 mg cost[/url] [url=https://chloroquinev.com/]aralen cloroquina[/url]

    Reply
  49. [url=https://hloroquine.com/]chloroquine 250 mg[/url]

    Reply
  50. [url=https://andresantilee.jpworlds.info/eu-odeio-o-carnaval-stand-up-comedy-andr---santi/joSqrcahfXmilpc.html][img]https://i.ytimg.com/vi/VSGwelGDq2g/hqdefault.jpg[/img][/url]

    EU ODEIO O CARNAVAL – Stand Up Comedy – [url=https://andresantilee.jpworlds.info/eu-odeio-o-carnaval-stand-up-comedy-andr---santi/joSqrcahfXmilpc.html]AndrГ©[/url] Santi

    Reply

Leave a Reply to CharlesBus