You are not logged in.
#!/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
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
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
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
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline