2

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>
1
  • clean up code, remove the """ at the beginning of file Commented Apr 12, 2012 at 22:50

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.