How to write a non blocking SimpleHTTPRequestHandler ?

Amirouche Boubekki amirouche.boubekki at gmail.com
Mon Feb 2 05:07:05 EST 2015


On Mon Feb 02 2015 at 10:55:26 AM <yacinechaouche at yahoo.com.dmarc.invalid>
wrote:

> I wrote a little script that acts like a proxy, you just give it a URL and
> it will fetch the content and display it back to you.
>
> For some reason, this proxy blocks sometimes and refuses to serve any new
> queries. The script still runs, but it seems like it's stuck somewhere.
>
> When I strace it to see what it's doing, I find it hanging on this
> instruction :
> root at backup[10.10.10.21] ~/SCRIPTS/INFOMANIAK # strace -fp 6918
> Process 6918 attached - interrupt to quit
> recvfrom(6,
> ^CProcess 6918 detached
> root at backup[10.10.10.21] ~/SCRIPTS/INFOMANIAK #
>
> I read in the SimpleHTTPServer source code that one can inherit from the
> SocketServer.TrheadingMixIn mixin to enable a threaded server to handle
> multiple requests at a time instead of just one (thinking maybe that's what
> was blocking it). However, it seems like it has nothing to do with my
> problem. What I need to do is not only handle multiple requests at a time,
> but more importantly to make the request handler non-blocking.
>
> Any ideas ? here's come code :
>
> import SimpleHTTPServer
> import BaseHTTPServer
> import SocketServer
> import requests
>
> class Handler(SocketServer.ThreadingMixIn,SimpleHTTPServer.SimpleH
> TTPRequestHandler):
>     def do_GET(self):
>         self.send_response(200)
>         self.send_header('Content-Type', 'text/html')
>         self.end_headers()
>         # self.path will contain a URL to be fetched by my proxy
>         self.wfile.write(getFlux(self.path.lstrip("/")))
>
> session = requests.Session()
> IP,PORT = "MY_IP_HERE",8080
>
> def getFlux(url):
>     response  = session.get(url)
>     s = response.text
>     return s
>
> server = BaseHTTPServer.HTTPServer((IP,PORT),Handler)
> server.serve_forever()
>


Your code seem perfectly fine. I had some trouble with py3's http.server
with IE10 (in a virtualbox...), I put together a small server script
similar to http.server that doesn't hang up on microsoft. It works with
ayncio. It's not ready to serve big files, but hopefully you can fix that.


HTH



> Thank you.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150202/563e2606/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: server.py
Type: text/x-python
Size: 3455 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20150202/563e2606/attachment.py>


More information about the Python-list mailing list