简体   繁体   中英

python httpserver on separate thread

I am trying to create an httpserver on a separate thread which should process a single get request and shutdown if a single parameter is passed in the url.

import sys
import threading
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

class WebServer(SimpleHTTPRequestHandler,threading.Thread):
    port = 9000
    protocol_version = "HTTP/1.1"
    code = ""        
    httpd = None
    isRunning = False
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type","text/html")
        self.end_headers()
        self.wfile.write("webpage")
        try:
            self.code = split(self.path,"=")[1]
        except:
            pass
        else:
            self.isRunning=False
            self.stop()            
    def do_POST(self):
        pass
    def __init__(self,port=9000):
        threading.Thread.__init__(self)
        self.port=port
    def run(self):
        while True:
            server_address = ("127.0.0.1",self.port)
            try:
                self.httpd = BaseHTTPServer.HTTPServer(server_address,WebServer)
            except:
                self.port +=1
            else:
                break
        self.isRunning=True
        self.httpd.serve_forever()

server = WebServer(1)
server.start()
while(server.isRunning==False):
    pass
print "Server running on port: %d" %(server.port)
server.join()
print "code: "+server.code

But an error occurs when a request is sent to it:

Server running on port: 1025
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56462)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
TypeError: __init__() takes at most 2 arguments (4 given)
----------------------------------------

when using curl to connect to the server,it gives this error:

curl: (52) Empty reply from server 

you're trying to do too many things with one class.

the way that the SimpleHTTPRequestHandler works is that the BaseHTTPServer creates a new instance to handle each request. it does that by calling the constructor. but you have added a new constructor which you are using to create the server itself.

is that clear?

when the BaseHttpServer receives a request it takes the WebServer class (which you pass in as a subclass of SimpleHTTPRequestHandler where you create self.httpd) and tries to create an instance. it expects the constructor for the SimpleHTTPRequestHandler subclass to have four arguments, but you've added a new constructor which takes only two (and is intended to create a web server, not a handler).

it's weird because you're probably not used to things creating a new instance. but that's how it works.

so separate your server and your request handler into two separate classes (as kichik says).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM