0

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 
1
  • Have you tried it with two separate classes? Create one class for the thread that creates a new instance of your HTTP server class. Also take a look at Twisted. Commented Jul 14, 2013 at 22:46

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! The server works now, but is it possible to shutdown the server from the handler class inside the do_GET method?
that's probably best as a separate question (i can't remember how off the top of my head, sorry).

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.