You are not logged in.

#1 2013-07-02 21:57:40

Super Root
Member
Registered: 2012-08-04
Posts: 23

Python HTTPServer and custom functions

#!/usr/bin/env python2

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class ServerHandler (BaseHTTPRequestHandler):
	
	def do_GET(self):
		
		self.send_response(200)
		self.send_header("Content-type", "text/html")
		self.send_header("Access-Control-Allow-Origin", "*")
		self.end_headers()
		
		getvars = self.path[1:].split("==DATA==")
		
		self.wfile.write(getvars)
	
	def test(self):
		
		print("TEST!")

def main():
	
	try:
		
		server = HTTPServer(("", 8000), ServerHandler)
		
		print("HTTP server started!")
		
		server.serve_forever()
	
	except KeyboardInterrupt:
		
		print("Keyboard interrupt received! Shutting down ..")
		
		server.socket.close()

if __name__ == "__main__":
	
	main()

What I need is a class which I can use for the server itself (parsing GET arguments) and calling custom functions (sending data to the client from another object/class.

Is there any way I can do this?

Note: test function does not work.


Simplicity is prerequisite for reliability.   -- Edsger Dijkstra

Offline

#2 2013-07-02 22:11:34

Nisstyre56
Member
From: Canada
Registered: 2010-03-25
Posts: 85

Re: Python HTTPServer and custom functions

Is there a good reason  you aren't using something like twisted or the http sever included in CherryPy?

If you absolutely insist in writing your own HTTP server you may want to read this: http://en.wikipedia.org/wiki/Reactor_pattern but Twisted already implements this for you, and with tons of stuff to correctly handle HTTP already.

Last edited by Nisstyre56 (2013-07-02 22:12:53)


In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage

Offline

#3 2013-07-02 22:21:39

Super Root
Member
Registered: 2012-08-04
Posts: 23

Re: Python HTTPServer and custom functions

Nisstyre56 wrote:

Is there a good reason  you aren't using something like twisted or the http sever included in CherryPy?

If you absolutely insist in writing your own HTTP server you may want to read this: http://en.wikipedia.org/wiki/Reactor_pattern but Twisted already implements this for you, and with tons of stuff to correctly handle HTTP already.

Two reasons:

- I've never heard of it;
- I'd really like to build one from scratch.

Actually, considering the fact that the app won't be receiving multiple connections at a time, HTTPServer and do_GET WOULD be enough for me. All I need is to receive some data and report back if necessary, but receiving AND sending data should be done through another custom function which I could call from a child object (class).

Last edited by Super Root (2013-07-02 22:22:56)


Simplicity is prerequisite for reliability.   -- Edsger Dijkstra

Offline

#4 2013-07-02 22:30:31

Nisstyre56
Member
From: Canada
Registered: 2010-03-25
Posts: 85

Re: Python HTTPServer and custom functions

Super Root wrote:

Two reasons:

- I've never heard of it;
- I'd really like to build one from scratch.

Actually, considering the fact that the app won't be receiving multiple connections at a time, HTTPServer and do_GET WOULD be enough for me. All I need is to receive some data and report back if necessary, but receiving AND sending data should be done through another custom function which I could call from a child object (class).

The reactor pattern does handle everything once at a time. The useful part of it is that you have "events" which you associate with callback functions which handle various types of requests and also handle responding. This *can* run in a multi-threaded environment, but it's unnecessary. The only thing you need to take care of is making sure that your callbacks do not block for a long period of time if you decide to do this as a single thread.


In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage

Offline

#5 2013-07-02 22:31:21

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Python HTTPServer and custom functions

Instantiate the child object in the server's __init__ method and store it in a server attribute. You can then access it with self.server.foo from the request handler, where foo is the name of the attribute that you chose.

You may find some useful code snippets for "GET" argument parsing (with proper URI parsing) and other things here.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2013-07-03 13:06:41

Super Root
Member
Registered: 2012-08-04
Posts: 23

Re: Python HTTPServer and custom functions

Is there any way I can access wfile (self.wfile.write) from a function outside of servers __init__?


Simplicity is prerequisite for reliability.   -- Edsger Dijkstra

Offline

#7 2013-07-03 21:16:50

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Python HTTPServer and custom functions

You can pass "self.wfile.write", "self.wfile", or just "self" to any other function or method, e.g.

# in request handler
def do_GET(self):
    ...
    foo(self)


# outside of request handler
def foo(request_handler):
    request_handler.wfile.write("Hello from foo!\n")

or

# in request handler
def do_GET(self):
    ...
    foo(self.wfile.write)


# outside of request handler
def foo(write):
    write("Hello from foo!\n")

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB