rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Apr 15
2009

Simple Webserver in Python digg

This snippet illustrates how one can easily build a HTTP Web Server in python. self.args will contain the query parameters.

WebServer.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Simple WebServer Illustration
# Pravin Paratey (April 15, 2009) [pravinp at gmail dot com]
 
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
 
class MyHandler(BaseHTTPRequestHandler):
    binaryExtensions = ['.gif', '.png', '.jpg']
    contentTypes = {
        '.css': 'text/css',
        '.gif': 'image/gif',
        '.jpg': 'image/jpg',
        '.png': 'image/png',
        'html': 'text/html',
    }
 
    def do_GET(self):
        """ Implementing the GET method """
        try:
            if self.path == '/': self.path = '/index.html'
            mode = 'r'
            if self.path[-4:] in self.binaryExtensions: mode = 'rb'
            fp = open(self.path[1:], mode)
            data = fp.read()
            fp.close()
            # Send response
            self.send_response(200)
            self.send_header('Content-Type', self.__getContentType())
            self.send_header('Transfer-Encoding', 'chunked')
            self.end_headers()
            self.wfile.write(data)
        except IOError:
            self.send_error(404, "File not found: %s" % self.path)
 
    def __getContentType(self):
        """ Function to figure out content types """
        content_type = 'text/plain'
        extension = self.path[-4:]
        if extension in self.contentTypes:
            content_type = self.contentTypes[extension]            
        return content_type
 
if __name__ == '__main__':
    server = HTTPServer(('', 8000), MyHandler)
    server.serve_forever()

One Response (rss) (trackback)

#1

Adam Tauno Williams

January 14th, 2010 at 3:59 pm

This isn’t correct. BaseHTTPRequestHandler defaults to HTTP/1.0 which doesn’t do chunked encoding (RFC2616), and even though Transfer-Encoding: chunked is specified – this script doesn’t do chunked encoding. It serves just a simple HTTP/1.0 non-pipelined GET and disconnect.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

Deebot

Deebot

Deeb0t is an IRC chat bot capable of making meaningful conversation with other users. It also responds to commands issued by its owner.

[Read More]

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.