convergedtarkus
convergedtarkus

Reputation: 697

Manually Implementing HTTP GET in python

I'm trying to implement a simple HTTP webserver in python for a class, and I'm stuck. Currently I've got it hardcoded to make things easier, but I can't figure out why it isn't working.

#Python 2
import socket
import threading

class socketEcho(threading.Thread):
  def __init__(self,conn):
    super(socketEcho,self).__init__()
    self.conn = conn

  def run(self):
    while 1:
      data = self.conn.recv(1024)
      if not data: 
        print 'Break'
        break
      data = """GET /index.html HTTP/1.1 
      host: www.esqsoft.globalservers.com

      """
      dataList = data.split()
      URI = dataList[1]
      URI = URI[1:]
      hostAddress = dataList[4]    
      try:
        file = open(URI,'r').read()
        result = "HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n"
        result = result + file
      except IOError as e: #HTTP 404
        print "Error"
        pass    
      #conn.send(data)
      print "Sending"
      print result
      try:
        self.conn.send(result)
      except:
        print "Send Error"
    HOST = ''
    PORT = 50420
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST,PORT))
    s.listen(2) #Blocks, accept only one connection
    while True:
      newSock, addr = s.accept()
      print 'Connected by', addr
      newConn = socketEcho(newSock)
      newConn.start()

When I try to send my borswer (firefox) to localhost:50420 it gets the 200 OK code just fine, but then it just waits there and never displays the page. And for i've looked at the print out of the result variable and it looks just fine, so I have no idea what is up, any suggestions? And here is the print out of the result variable just before it is sent.

HTTP/1.1 200 OK 
Content-Type: text/html

<html>

    <head>
    </head>

    <body>

        <h1>Hello World</h1>
        <p>Yes, this does indeed work</p>
        <p><a href="http://www.google.com">Google</a></p>
        <!--<a href="http://knuth.luther.edu/~ranuha01/hello.html">Link Test</a>-->
        <!--<img src="earth.jpg" alt="Earth picture" height="100">-->
        <ul>
            <li><b>One</b></li>
            <ul>
                <li>One and a half</li>
            </ul>
            <li><b>Two</b></li>
        </ul>

    </body>

</html>

Upvotes: 2

Views: 1119

Answers (1)

WooParadog
WooParadog

Reputation: 649

First, you should close the conn in the handler; second, don't put a loop in run(self), event loop is in the main.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Python 2
import socket
import threading

class socketEcho(threading.Thread):
  def __init__(self,conn):
      super(socketEcho,self).__init__()
      self.conn = conn

  def run(self):
      data = self.conn.recv(1024)
      if not data: 
        print 'Break'
        self.conn.close()
        return
      data = """GET /index.html HTTP/1.1 
      host: www.esqsoft.globalservers.com

      """
      dataList = data.split()
      URI = dataList[1]
      URI = URI[1:]
      hostAddress = dataList[4]    
      try:
        file = open(URI,'r').read()
        result = "HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n"
        result = result + file
      except IOError as e: #HTTP 404
        print "Error"
        pass    
      #conn.send(data)
      print "Sending"
      print result
      try:
        self.conn.send(result)
        self.conn.close()
      except:
        print "Send Error"

HOST = ''
PORT = 50420
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(2) #Blocks, accept only one connection
while True:
  newSock, addr = s.accept()
  print 'Connected by', addr
  newConn = socketEcho(newSock)
  newConn.start()

Upvotes: 1

Related Questions