2

我正在尝试在 python 中为一个类实现一个简单的 HTTP 网络服务器,但我被卡住了。目前我已经对其进行了硬编码以使事情变得更容易,但我无法弄清楚为什么它不起作用。

#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()

当我尝试将我的 borswer (firefox) 发送到 localhost:50420 时,它会得到 200 OK 代码,但它只是在那里等待并且从不显示页面。因为我查看了结果变量的打印结果,它看起来很好,所以我不知道发生了什么,有什么建议吗?这是结果变量在发送之前的打印结果。

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>
4

1 回答 1

1

首先,您应该关闭处理程序中的 conn;其次,不要在run(self)中放置循环,事件循环在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()
于 2012-04-13T01:08:33.050 回答