On Mon Feb 02 2015 at 10:55:26 AM <yacinechaou...@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@backup[10.10.10.21] ~/SCRIPTS/INFOMANIAK # strace -fp 6918 > Process 6918 attached - interrupt to quit > recvfrom(6, > ^CProcess 6918 detached > root@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 >
#!/usr/bin/env python3 import os import mimetypes from asyncio import coroutine from asyncio import get_event_loop from aiohttp.web import Application from aiohttp.web import StaticRoute from aiohttp.web import HTTPNotFound from aiohttp.web import HTTPMovedPermanently from aiohttp.web import StreamResponse class StaticRouteWithIndex(StaticRoute): limit = 8192 @coroutine def handle(self, request): resp = StreamResponse() filename = request.match_info['filename'] filepath = os.path.join(self._directory, filename) if '..' in filename: print('not found %s' % filepath) raise HTTPNotFound() if not os.path.exists(filepath): print('not found %s' % filepath) raise HTTPNotFound() if not os.path.isfile(filepath): directory = filepath filename = None if not filepath.endswith('/'): path = filepath + '/' path = path[len(self._directory):] raise HTTPMovedPermanently(path) for index in ('index.html', 'index.htm'): path = os.path.join(directory, index) if os.path.exists(path): filename = index filepath = path break if not filename and os.path.isdir(filepath): if not filepath.endswith('/'): filepath += '/' names = os.listdir(filepath) names.sort() output = '<ul>' for name in names: dirname = os.path.join(filepath, name) if os.path.isdir(dirname): dirname += '/' path = dirname[len(self._directory):] link = '<a href="%s">%s</a>' % (path, name) output += '<li>' + link + '</li>' output += '</ul>' resp.content_type = 'text/html' resp.start(request) resp.write(output.encode('utf-8')) return resp elif not filename: print('not found %s' % filepath) raise HTTPNotFound() else: ct = mimetypes.guess_type(filename)[0] if not ct: ct = 'application/octet-stream' resp.content_type = ct file_size = os.stat(filepath).st_size single_chunk = file_size < self.limit if single_chunk: resp.content_length = file_size resp.start(request) with open(filepath, 'rb') as f: chunk = f.read(self.limit) if single_chunk: resp.write(chunk) else: while chunk: resp.write(chunk) chunk = f.read(self.limit) print('ok %s' % filepath) return resp @coroutine def init(loop): path = os.path.abspath(os.curdir) app = Application(loop=loop) route = StaticRouteWithIndex(None, '/', path) app.router._register_endpoint(route) handler = app.make_handler() srv = yield from loop.create_server(handler, '0.0.0.0', 8001) return srv, handler loop = get_event_loop() srv, handler = loop.run_until_complete(init(loop)) print("Server started at http://127.0.0.1:8001") try: loop.run_forever() except KeyboardInterrupt: loop.run_until_complete(handler.finish_connections())
-- https://mail.python.org/mailman/listinfo/python-list