Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Post Code

Name: Anonymous 2011-03-04 21:33

One recurring complaint is that nobody talks about code on /prog/. So everyone write go and write some code, any code, that does something, anything, and post it. A small explanation wouldn't go amiss either.

Name: Anonymous 2011-03-20 22:08

Here's a simple Python webserver I just wrote. It currently only supports GET requests.

#
# httpd.py
#

import socket, sys, thread

def do_request(method, path, headers):
  if method == 'get':
    # handle get request
    if path == '/':
      path = '/index.html'

    # open file
    try:
      f = open(root + path, 'r')

      # opened file successfully
      response = 'HTTP/1.0 200 OK\r\n'

      # TODO: add needed headers

      response += '\r\n'

      # send file contents
      for line in f:
        response += line.rstrip() + '\r\n'

      return response

    except IOError:
      # send 404 response
      return 'HTTP/1.0 404 Not Found\r\n'

  else:
    return ''

def handle(conn, addr):
  # create a file object for the socket
  f = conn.makefile()
  conn.close()

  line = f.readline().lower().strip().split()

  while True:
    # read request
    method, path, http_ver = line

    # read headers
    headers = []
    h = f.readline().lower().strip().split()

    while h != []:
      headers += [(h[0], ''.join(h[1:]))]
      h = f.readline().lower().strip().split()

    # handle request
    f.write(do_request(method, path, headers))

    # check whether to keep the connection open
    if http_ver != 'http/1.1' or ('connection:', 'close') in headers or 'keep-alive:' in map(lambda (a, b): a, headers):
      break

    # get rid of empty lines before the next request
    line = []

    while line == []:
      line = f.readline().lower().strip().split()

  # close file and return
  f.close()

# default settings
host = ''
port = 8000
root = '.'

# parse command line arguments
argv = sys.argv[1:]

while argv:
  if argv[0] == '-h':
    # print help and exit
    print 'options: -h display this help'
    print ' -p [port] set port'
    print ' -H [hostname] set hostname'
    print ' -r [dir] set root directory'
    exit()

  elif argv[0] == '-p':
    # set port number
    if len(argv) == 1:
      print '\'-p\' requires an argument'
      exit()

    port = int(argv[1])
    argv = argv[2:]

    continue

  elif argv[0] == '-H':
    # set hostname
    if len(argv) == 1:
      print '\'-H\' requires an argument'
      exit()

    host = argv[1]
    argv = argv[2:]

    continue

  elif argv[0] == '-r':
    # set root directory
    if len(argv) == 1:
      print '\'-r\' requires an argument'
      exit()

    root = argv[1]
    argv = argv[2:]

# open a port
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.bind((host, port))
conn.listen(1)

# accept requests and handle in a new thread
while True:
  thread.start_new_thread(handle, conn.accept())

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List