def setUp(self): self.logfile = StringIO() self.victim = api.spawn(wsgi.server, api.tcp_listener(('0.0.0.0', 31337)), self.site_class(), log=self.logfile, max_size=128)
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option("-l", "--listen", dest="host", default="0.0.0.0", help="the ip interface to bind") parser.add_option("-p", "--port", default=7902, type=int, help="which port to listen") parser.add_option("-d", "--daemon", action="store_true", help="run in daemon", default=False) (options, args) = parser.parse_args() cfg = {"localhost:7901":range(16), "localhost:7902":range(16), "localhost:7903":range(16)} store = Client(cfg, 16) print "server listening on %s:%s" % (options.host, options.port) server = api.tcp_listener((options.host, options.port)) util.set_reuse_addr(server) while True: try: new_sock, address = server.accept() except KeyboardInterrupt: break api.spawn(handler, store, new_sock) print 'close listener ...' server.close()
def serve(self): self._running = True socket = evtapi.tcp_listener((self.host,self.port)) while self._running: client, _ = socket.accept() Dispatcher(client,self.mapper).dispatch() socket.close()
def main(): from optparse import OptionParser parser = OptionParser(usage="usage: %prog [options]") parser.add_option("-D", "--debug", dest="debug", help="debug mode.", action="store_true", default=False) parser.add_option("-c", "--cpool", dest="cpool_size", help="size of coroutine pool.", metavar="<size>", type="int", default=512) opts, args = parser.parse_args() sockets = [] pool = coros.CoroutinePool(max_size=opts.cpool_size) if len(args) == 0: sockets.append(fcgi_stdin_sock()) else: for arg in args: # todo: add support for UNIX sockets addr = arg.replace('[','').replace(']','').rsplit(':', 1) if len(addr) != 2: parser.error('arguments must be IPv4 or IPv6 addresses including port') addr = (addr[0], int(addr[1])) sockets.append(api.tcp_listener(addr)) run(sockets, handle_connection, pool)
def test_timeout_cancel(self): server = api.tcp_listener(('0.0.0.0', 0)) bound_port = server.getsockname()[1] def client_connected((conn, addr)): conn.close() def go(): client = util.tcp_socket() desc = greenio.GreenSocket(client) desc.connect(('127.0.0.1', bound_port)) try: api.trampoline(desc, read=True, write=True, timeout=0.1) except api.TimeoutError: assert False, "Timed out" server.close() client.close() api.call_after(0, go) api.tcp_server(server, client_connected) check_hub()
def test_del_closes_socket(self): timer = api.exc_after(0.5, api.TimeoutError) def accept_once(listener): # delete/overwrite the original conn # object, only keeping the file object around # closing the file object should close everything try: conn, addr = listener.accept() conn = conn.makeGreenFile() conn.write('hello\n') conn.close() self.assertRaises(socket.error, conn.write, 'a') finally: listener.close() server = api.tcp_listener(('0.0.0.0', 0)) killer = api.spawn(accept_once, server) client = api.connect_tcp(('127.0.0.1', server.getsockname()[1])) fd = client.makeGreenFile() client.close() assert fd.read() == 'hello\n' assert fd.read() == '' timer.cancel()
def main(): from optparse import OptionParser parser = OptionParser(usage="usage: %prog [options]") parser.add_option("-D", "--debug", dest="debug", help="debug mode.", action="store_true", default=False) parser.add_option("-c", "--cpool", dest="cpool_size", help="size of coroutine pool.", metavar="<size>", type="int", default=512) opts, args = parser.parse_args() sockets = [] pool = coros.CoroutinePool(max_size=opts.cpool_size) if len(args) == 0: sockets.append(fcgi_stdin_sock()) else: for arg in args: # todo: add support for UNIX sockets addr = arg.replace('[', '').replace(']', '').rsplit(':', 1) if len(addr) != 2: parser.error( 'arguments must be IPv4 or IPv6 addresses including port') addr = (addr[0], int(addr[1])) sockets.append(api.tcp_listener(addr)) run(sockets, handle_connection, pool)
def serve(self): self._running = True socket = evtapi.tcp_listener((self.host, self.port)) while self._running: client, _ = socket.accept() Dispatcher(client, self.mapper).dispatch() socket.close()
def test_close_with_makefile(self): def accept_close_early(listener): # verify that the makefile and the socket are truly independent # by closing the socket prior to using the made file try: conn, addr = listener.accept() fd = conn.makeGreenFile() conn.close() fd.write('hello\n') fd.close() self.assertRaises(socket.error, fd.write, 'a') self.assertRaises(socket.error, conn.send, 'b') finally: listener.close() def accept_close_late(listener): # verify that the makefile and the socket are truly independent # by closing the made file and then sending a character try: conn, addr = listener.accept() fd = conn.makeGreenFile() fd.write('hello') fd.close() conn.send('\n') conn.close() self.assertRaises(socket.error, fd.write, 'a') self.assertRaises(socket.error, conn.send, 'b') finally: listener.close() def did_it_work(server): client = api.connect_tcp(('127.0.0.1', server.getsockname()[1])) fd = client.makeGreenFile() client.close() assert fd.readline() == 'hello\n' assert fd.read() == '' fd.close() server = api.tcp_listener(('0.0.0.0', 0)) killer = api.spawn(accept_close_early, server) did_it_work(server) api.kill(killer) server = api.tcp_listener(('0.0.0.0', 0)) killer = api.spawn(accept_close_late, server) did_it_work(server) api.kill(killer)
def run(app, host='', port=8081): try: wsgi.server(api.tcp_listener((host, port)), app) except KeyboardInterrupt: print '>>> exiting cleanly...' return 0 finally: return -1
def setUp(self): self.logfile = StringIO() self.site = Site() self.killer = api.spawn(wsgi.server, api.tcp_listener(('0.0.0.0', 12346)), self.site, max_size=128, log=self.logfile)
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option("-l", "--listen", dest="host", default="0.0.0.0", help="the ip interface to bind") parser.add_option("-p", "--port", default=7902, type=int, help="which port to listen") # parser.add_option("-d", "--daemon", action="store_true", # help="run in daemon", default=False) parser.add_option("-H", "--home", default="beansdb", help="the database path") parser.add_option("-c", "--count", default=16, type=int, help="number of db file, power of 16") parser.add_option("-s", "--start", default=0, type=int, help="start index of db file") parser.add_option("-e", "--end", default=-1, type=int, help="last end of db file, -1 means no limit") parser.add_option("-n", "--limit", default=100, type=int, help="diffs limit to do db scan") parser.add_option("-t", "--threads", type=int, default=20, help="number of IO threads") (options, args) = parser.parse_args() store = (HStore(options.home, int(math.log(options.count, 16)), options.start, options.end)) #store.check(options.limit, nonblocking=True) api.spawn(tpool.execute, store.check, options.limit) # check in thread pool api.spawn(tpool.execute, flush, store) print "server listening on %s:%s" % (options.host, options.port) server = api.tcp_listener((options.host, options.port)) util.set_reuse_addr(server) while True: try: new_sock, address = server.accept() except KeyboardInterrupt: break api.spawn(handler, store, new_sock, new_sock.makefile('r'), new_sock.makefile('w')) global quit quit = True print 'close listener ...' server.close() print 'stop checker thread ...' store.stop_check() print 'stop worker threads ...' tpool.killall() print 'close store...' store.close()
def main(): class deadlog(object): def write(self, s, l=0): pass httpd.server(api.tcp_listener(('127.0.0.1', 8090)), Handler(), max_size=5000, log=deadlog())
def serve(self, port=('localhost', 4000)): from web import Server try: from eventlet import wsgi, api except ImportError: print "Unnable to find eventlet, easy_install eventlet and try again." return sock = api.tcp_listener(port) wsgi.server(sock, Server(self)) return sock
def main(): try: server=api.tcp_listener(('0.0.0.0',3000)) print 'Server started!' while True: conn,addr=server.accept() #print 'client %s connected!'%repr(addr) writer=conn.makefile('w') api.spawn(httpd,writer,conn.makefile('r')) except KeyboardInterrupt: pass return
def run(self): HOST = '' PORT = 8089 config = self.path+"/python_paste.ini" appname = "common" print os.popen('pwd').readlines() wsgi_app = loadapp("config:%s" % os.path.abspath(config), appname) from eventlet import api,wsgi print 'Starting up HTTP server on %s: %i..........' %(HOST,PORT) wsgi.server(api.tcp_listener((HOST,PORT)),wsgi_app)
def serve(self, filename, conf, error): """Run the publisher In: - ``filename`` -- the path to the configuration file - ``conf`` -- the ``ConfigObj`` object, created from the configuration file - ``error`` -- the function to call in case of configuration errors """ host, port, conf = self._validate_conf(filename, conf, error) # The publisher is an events based server so call once the ``on_new_process()`` method self.on_new_process() wsgi.server(api.tcp_listener((host, port)), self.urls)
def test_001_trampoline_timeout(self): server = api.tcp_listener(('0.0.0.0', 0)) bound_port = server.getsockname()[1] try: desc = greenio.GreenSocket(util.tcp_socket()) desc.connect(('127.0.0.1', bound_port)) api.trampoline(desc, read=True, write=False, timeout=0.1) except api.TimeoutError: pass # test passed else: assert False, "Didn't timeout" check_hub()
def bind_socket(config): sleeptime = 0.5 host = config.get('host', '') port = config.get('port', 8080) for x in range(8): try: sock = api.tcp_listener((host, port)) break except socket.error, e: if e[0] != errno.EADDRINUSE: raise print "(%s) socket %s:%s already in use, retrying after %s seconds..." % ( os.getpid(), host, port, sleeptime) api.sleep(sleeptime) sleeptime *= 2
def serve(self, interface='0.0.0.0', port=3080, max_size=DEFAULT_MAX_CONC_REQUESTS): self.log.info('ZenQueue HTTP Server v%s', zenqueue.__version__) if interface == '0.0.0.0': self.log.info('Serving on %s:%d (all interfaces)', interface, port) else: self.log.info('Serving on %s:%d', interface, port) self.sock = api.tcp_listener((interface, port)) try: # Wrap `self` with `Request.application` so that we get a request as # an argument instead of the usual `environ, start_response`. wsgi.server(self.sock, Request.application(self), max_size=max_size) finally: self.sock = None
def test_ssl_proxy_redirects(self): # make sure that if the proxy returns a redirect, that httpc # successfully follows it (this was broken at one point) def ssl_proxy(sock): conn, addr = sock.accept() fd = conn.makefile() try: line = request = fd.readline() self.assertEqual(request, 'GET https://localhost:1234 HTTP/1.1\r\n') while line.strip(): # eat request headers line = fd.readline() # we're not going to actually proxy to localhost:1234, # we're just going to return a response on its behalf fd.write( "HTTP/1.0 302 Found\r\nLocation: https://localhost:1234/2\r\n\r\n" ) finally: fd.close() conn.close() # second request, for /2 target conn, addr = sock.accept() fd = conn.makefile() try: line = request = fd.readline() self.assertEqual(request, 'GET https://localhost:1234/2 HTTP/1.1\r\n') while line.strip(): # eat request headers line = fd.readline() fd.write("HTTP/1.0 200 OK\r\n\r\n") finally: fd.close() conn.close() sock.close() server = api.tcp_listener(('0.0.0.0', 5505)) api.spawn(ssl_proxy, server) import os os.environ['ALL_PROXY'] = 'localhost:5505' httpc.get('https://localhost:1234', use_proxy=True, max_retries=1)
def test_server(self): connected = [] server = api.tcp_listener(('0.0.0.0', 0)) bound_port = server.getsockname()[1] def accept_twice((conn, addr)): connected.append(True) conn.close() if len(connected) == 2: server.close() api.call_after(0, api.connect_tcp, ('127.0.0.1', bound_port)) api.call_after(0, api.connect_tcp, ('127.0.0.1', bound_port)) try: api.tcp_server(server, accept_twice) except: api.sleep(0.1) raise assert len(connected) == 2 check_hub()
def serve(self, interface='0.0.0.0', port=3000): self.log.info('ZenQueue Native Server v%s', zenqueue.__version__) if interface == '0.0.0.0': self.log.info('Serving on %s:%d (all interfaces)', interface, port) else: self.log.info('Serving on %s:%d', interface, port) self.socket = api.tcp_listener((interface, port)) # A lot of the code below was copied or adapted from eventlet's # implementation of an asynchronous WSGI server. try: while True: try: try: client_socket, client_addr = self.socket.accept() except socket.error, exc: # EPIPE (Broken Pipe) and EBADF (Bad File Descriptor) # errors are common for clients that suddenly quit. We # shouldn't worry so much about them. if exc[0] not in [errno.EPIPE, errno.EBADF]: raise # Throughout the logging output, we use the client's ID in # hexadecimal to identify a particular client in the logs. self.log.info('Client %x connected: %r', id(client_socket), client_addr) # Handle this client on the pool, sleeping for 0 time to # allow the handler (or other coroutines) to run. self.client_pool.execute_async(self.handle, client_socket) api.sleep(0) except KeyboardInterrupt: # It's a fatal error because it kills the program. self.log.fatal('Received keyboard interrupt.') # This removes the socket from the current hub's list of # sockets to check for clients (i.e. the select() call). # select() is a key component of asynchronous networking. api.get_hub().remove_descriptor(self.socket.fileno()) break
def test_ssl_proxy(self): def ssl_proxy(sock): conn, addr = sock.accept() fd = conn.makefile() try: line = request = fd.readline() self.assertEqual(request, 'GET https://localhost:1234 HTTP/1.1\r\n') while line.strip(): # eat request headers line = fd.readline() # we're not going to actually proxy to localhost:1234, # we're just going to return a response on its behalf fd.write("HTTP/1.0 200 OK\r\n\r\n") finally: fd.close() conn.close() server = api.tcp_listener(('0.0.0.0', 5505)) api.spawn(ssl_proxy, server) import os os.environ['ALL_PROXY'] = 'localhost:5505' httpc.get('https://localhost:1234', ok=[200], use_proxy=True)
def main(argv=None): if argv is None: argv = sys.argv s.connect() time.sleep(3) for i in range(0, 255): s.setSpeed(4, i) time.sleep(.005) for i in range(255, 0, -1): s.setSpeed(4, i) time.sleep(.005) server = api.tcp_listener(('localhost', 9000)) while True: try: new_sock, address = server.accept() except KeyboardInterrupt: break # handle every new connection with a new coroutine api.spawn(handle_socket, new_sock.makefile('r'), new_sock.makefile('w')) s.disconnect() return 0
def test_ssl_proxy_redirects(self): # make sure that if the proxy returns a redirect, that httpc # successfully follows it (this was broken at one point) def ssl_proxy(sock): conn, addr = sock.accept() fd = conn.makefile() try: line = request = fd.readline() self.assertEqual(request, 'GET https://localhost:1234 HTTP/1.1\r\n') while line.strip(): # eat request headers line = fd.readline() # we're not going to actually proxy to localhost:1234, # we're just going to return a response on its behalf fd.write("HTTP/1.0 302 Found\r\nLocation: https://localhost:1234/2\r\n\r\n") finally: fd.close() conn.close() # second request, for /2 target conn, addr = sock.accept() fd = conn.makefile() try: line = request = fd.readline() self.assertEqual(request, 'GET https://localhost:1234/2 HTTP/1.1\r\n') while line.strip(): # eat request headers line = fd.readline() fd.write("HTTP/1.0 200 OK\r\n\r\n") finally: fd.close() conn.close() sock.close() server = api.tcp_listener(('0.0.0.0', 5505)) api.spawn(ssl_proxy, server) import os os.environ['ALL_PROXY'] = 'localhost:5505' httpc.get('https://localhost:1234', use_proxy=True, max_retries=1)
def test_connect_tcp(self): def accept_once(listenfd): try: conn, addr = listenfd.accept() fd = conn.makeGreenFile() conn.close() fd.write('hello\n') fd.close() finally: listenfd.close() server = api.tcp_listener(('0.0.0.0', 0)) api.spawn(accept_once, server) client = api.connect_tcp(('127.0.0.1', server.getsockname()[1])) fd = client.makeGreenFile() client.close() assert fd.readline() == 'hello\n' assert fd.read() == '' fd.close() check_hub()
"""This is a simple example of running a wsgi application with eventlet. For a more fully-featured server which supports multiple processes, multiple threads, and graceful code reloading, see: http://pypi.python.org/pypi/Spawning/ """ from eventlet import api, wsgi def hello_world(env, start_response): if env['PATH_INFO'] != '/': start_response('404 Not Found', [('Content-Type', 'text/plain')]) return ['Not Found\r\n'] start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello, World!\r\n'] wsgi.server(api.tcp_listener(('', 8080)), hello_world)
def test_tcp_listener(self): socket = api.tcp_listener(('0.0.0.0', 0)) assert socket.getsockname()[0] == '0.0.0.0' socket.close() check_hub()
from eventlet import api from config import * def httpd(socket): while True: line = socket.recv(1024) if EOL1 in line or EOL2 in line: break socket.sendall(response) ser = api.tcp_listener(('0.0.0.0', 8080)) while True: conn, addr = ser.accept() api.spawn(httpd, conn)
#!/bin/python from paste.deploy import loadapp import os import sys sys.path.append('..') HOST = '' PORT = 8091 config = "python_paste.ini" appname = "common" wsgi_app = loadapp("config:%s" % os.path.abspath(config), appname) if __name__ == "__main__": from eventlet import api, wsgi print 'Starting up HTTP server on %s: %i..........' % (HOST, PORT) wsgi.server(api.tcp_listener((HOST, PORT)), wsgi_app) #server.serve_forever() # server.handle_request()
def setUp(self): self.logfile = StringIO() self.site = Site() self.killer = api.spawn( wsgi.server, api.tcp_listener(('0.0.0.0', 12346)), self.site, max_size=128, log=self.logfile)
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from eventlet import api def handle_socket(reader, writer): print "client connected" while True: # pass through every non-eof line x = reader.readline() if not x: break writer.write(x) print "echoed", x print "client disconnected" print "server socket listening on port 6000" server = api.tcp_listener(('0.0.0.0', 6000)) while True: try: new_sock, address = server.accept() except KeyboardInterrupt: break # handle every new connection with a new coroutine api.spawn(handle_socket, new_sock.makefile('r'), new_sock.makefile('w'))
hub.schedule_call_global(0, greenlet.switch) except socket.error, e: # Broken pipe means it was shutdown if e[0] != 32: raise finally: server.close() def backdoor((conn, addr), locals=None): """ Use this with tcp_server like so: api.tcp_server( api.tcp_listener(('127.0.0.1', 9000)), backdoor.backdoor, {}) """ host, port = addr print "backdoor to %s:%s" % (host, port) fl = conn.makeGreenFile("rw") fl.newlines = '\n' greenlet = SocketConsole(fl, (host, port), locals) hub = api.get_hub() hub.schedule_call_global(0, greenlet.switch) if __name__ == '__main__': api.tcp_server(api.tcp_listener(('127.0.0.1', 9000)), backdoor, {})
from router import Router from wsgi_app import Wsgi_Apps import ConfigParser config = ConfigParser.ConfigParser() config.readfp(open('../conf/sc.conf',"rb")) router = Router(config) wsgi_app = Wsgi_Apps(config) wsgi_app.init_router(router) HOST = '' PORT = 8089 if __name__ == "__main__": # from wsgiref.simple_server import make_server from eventlet import api,wsgi print 'Starting up HTTP server on %s: %i..........' %(HOST,PORT) wsgi.server(api.tcp_listener((HOST,PORT)),router) # server.handle_request()
self.close_connection = 1 return self.environ = fixup(h) self.requestline = "%s %s %s" % ( self.environ['REQUEST_METHOD'], self.environ['REQUEST_URI'], self.environ['HTTP_VERSION']) self.application = self.server.app try: self.server.outstanding_requests += 1 try: self.handle_one_response() except socket.error, e: # Broken pipe, connection reset by peer if e[0] in (32, 54): pass else: raise finally: self.server.outstanding_requests -= 1 def app(env, start_response): start_response('200 OK', [('Content-type', 'text/plain')]) return ["Hello, world\r\n"] wsgi.server(api.tcp_listener(('', 8889)), app, file('/dev/null', 'w'), protocol=MongrelProtocol) #wsgi.server(api.tcp_listener(('', 8889)), app, file('/dev/null', 'w'))
def eventlet_server(): "Run as standalone wsgi server" from eventlet import wsgi, api wsgi.server(api.tcp_listener(('', settings.SERVER_PORT)), Reactor)
#!/bin/python from paste.deploy import loadapp import os import sys sys.path.append('..') HOST = '' PORT = 8091 config = "python_paste.ini" appname = "common" wsgi_app = loadapp("config:%s" % os.path.abspath(config), appname) if __name__ == "__main__": from eventlet import api,wsgi print 'Starting up HTTP server on %s: %i..........' %(HOST,PORT) wsgi.server(api.tcp_listener((HOST,PORT)),wsgi_app) #server.serve_forever() # server.handle_request()
from eventlet import api from config import * def httpd(socket): while True: line = socket.recv(1024) if EOL1 in line or EOL2 in line: break socket.sendall(response) ser = api.tcp_listener(('0.0.0.0', 8080)) while True: conn,addr = ser.accept() api.spawn(httpd, conn)
from router import Router from wsgi_app import Wsgi_Apps import ConfigParser config = ConfigParser.ConfigParser() config.readfp(open('../conf/sc.conf', "rb")) router = Router(config) wsgi_app = Wsgi_Apps(config) wsgi_app.init_router(router) HOST = '' PORT = 8089 if __name__ == "__main__": # from wsgiref.simple_server import make_server from eventlet import api, wsgi print 'Starting up HTTP server on %s: %i..........' % (HOST, PORT) wsgi.server(api.tcp_listener((HOST, PORT)), router) # server.handle_request()