Пример #1
0
def start():
	evwsgi.start('0.0.0.0', '8080') 
	evwsgi.set_base_module(base)

	def proxy(baseurl):
		def f(environ, start_response):
			try:
				import urllib2
				resp = urllib2.urlopen(baseurl + environ["PATH_INFO"] + ("?" + environ["QUERY_STRING"] if environ["QUERY_STRING"] != "" else ""))
				if resp.code == 200:
					start_response('200 OK', [('Content-Type', resp.info().gettype())])
					return [resp.read()]
			except:
				start_response('403 ERROR', [('Content-Type', 'text/plain')])
				return ['Error loading request.']
		return f

	evwsgi.wsgi_cb(('/boundaries/', proxy("http://gis.govtrack.us/boundaries/")))
	evwsgi.wsgi_cb(('/map/tiles/', proxy("http://gis.govtrack.us/map/tiles/")))
	evwsgi.wsgi_cb(('/static/rep_photos/', proxy("http://www.govtrack.us/data/photos/")))
	
	staticfile = views.Staticfile('static', maxage=2629000)
	evwsgi.wsgi_cb(('/static', staticfile))

	staticfile = views.Staticfile('your_new_district.html', maxage=2629000)
	evwsgi.wsgi_cb(('/', staticfile))

	evwsgi.set_debug(0)	   
	evwsgi.run()
Пример #2
0
def start():
    evwsgi.start(
        os.getenv('FAVIEW_IP', '0.0.0.0'),
        os.getenv('FAVIEW_PORT', '8080'),
    )
    evwsgi.set_base_module(base)

    for local_path, real_path in MEDIA_PREFIX.iteritems():
        media_dir = ServeStatic(
            settings.MEDIA_ROOT + local_path,
            real_path,
            maxage=2629000,
        )
        evwsgi.wsgi_cb((
            settings.MEDIA_URL + local_path,
            media_dir,
        ))

    def generic(environ, start_response):
        res = django_handler.handler(environ, start_response)
        return [res]

    evwsgi.wsgi_cb(('', generic))
    evwsgi.set_debug(0)
    evwsgi.run()
Пример #3
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(fapws.base)
    evwsgi.wsgi_cb(("",generic))
    evwsgi.set_debug(0)    
    print "libev ABI version:%i.%i" % evwsgi.libev_version()
    evwsgi.run()
Пример #4
0
def start():
    if socket_server:
        evwsgi.start("\0/org/fapws3/server", "unix")
    else:
        evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(mybase)
    
 
    evwsgi.wsgi_cb(("/env", env))
    evwsgi.wsgi_cb(("/helloclass", helloclass("!!!")))
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/tuplehello", tuplehello))
    evwsgi.wsgi_cb(("/iterhello", iteration))
    evwsgi.wsgi_cb(("/longzipped", staticlongzipped))
    evwsgi.wsgi_cb(("/long", staticlong))
    evwsgi.wsgi_cb(("/elong", embedlong))
    evwsgi.wsgi_cb(("/short", staticshort))
    staticform=views.Staticfile("test.html")
    evwsgi.wsgi_cb(("/staticform", staticform))
    evwsgi.wsgi_cb(("/testpost", testpost))
    evwsgi.wsgi_cb(("/badscript", badscript))
    evwsgi.wsgi_cb(("/returnnone", returnnone))
    evwsgi.wsgi_cb(("/returnnull", returnnull))
    evwsgi.wsgi_cb(("/returniternull", returniternull))

    evwsgi.set_debug(0)    
    evwsgi.run()
Пример #5
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(base)
    
    def hello(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ["hello world!!"]

    @log.Log()
    def staticlong(environ, start_response):
        try:
            f=open("long.txt", "rb")
        except:
            f=["Page not found"]
        start_response('200 OK', [('Content-Type','text/html')])
        return f
    def embedlong(environ, start_response):
        try:
            c=open("long.txt", "rb").read()
        except:
            c=["Page not found"]
        start_response('200 OK', [('Content-Type','text/html')])
        return base.split_len(c,32768)
    def staticshort(environ, start_response):
        f=open("short.txt", "rb")
        start_response('200 OK', [('Content-Type','text/html')])
        return f
    def testpost(environ, start_response):
        print environ
        print "INPUT DATA",environ["wsgi.input"].getvalue()
        return ["OK. params are:%s" % (environ["fapws.params"])]

    @log.Log()
    @zip.Gzip()    
    def staticlongzipped(environ, start_response):
        try:
            f=open("long.txt", "rb")
        except:
            f=["Page not found"]
        start_response('200 OK', [('Content-Type','text/html')])
        return f
    def iteration(environ, start_response):
        start_response('200 OK', [('Content-Type','text/plain')])
        yield "hello"
        yield " "
        yield "worlds!!"

    
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/iterhello", iteration))
    evwsgi.wsgi_cb(("/longzipped", staticlongzipped))
    evwsgi.wsgi_cb(("/long", staticlong))
    evwsgi.wsgi_cb(("/elong", embedlong))
    evwsgi.wsgi_cb(("/short", staticshort))
    staticform=views.Staticfile("test.html")
    evwsgi.wsgi_cb(("/staticform", staticform))
    evwsgi.wsgi_cb(("/testpost", testpost))

    evwsgi.set_debug(0)    
    evwsgi.run()
Пример #6
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(fapws.base)
    evwsgi.wsgi_cb(("", generic))
    evwsgi.set_debug(0)
    print "libev ABI version:%i.%i" % evwsgi.libev_version()
    evwsgi.run()
Пример #7
0
def start():
    if socket_server:
        evwsgi.start("\0/org/fapws3/server", "unix")
    else:
        evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(mybase)

    evwsgi.wsgi_cb(("/env", env))
    evwsgi.wsgi_cb(("/helloclass", helloclass("!!!")))
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/tuplehello", tuplehello))
    evwsgi.wsgi_cb(("/iterhello", iteration))
    evwsgi.wsgi_cb(("/longzipped", staticlongzipped))
    evwsgi.wsgi_cb(("/long", staticlong))
    evwsgi.wsgi_cb(("/elong", embedlong))
    evwsgi.wsgi_cb(("/short", staticshort))
    staticform = views.Staticfile("test.html")
    evwsgi.wsgi_cb(("/staticform", staticform))
    evwsgi.wsgi_cb(("/testpost", testpost))
    evwsgi.wsgi_cb(("/badscript", badscript))
    evwsgi.wsgi_cb(("/returnnone", returnnone))
    evwsgi.wsgi_cb(("/returnnull", returnnull))
    evwsgi.wsgi_cb(("/returniternull", returniternull))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #8
0
def start():
    evwsgi.start(
        os.getenv('FAVIEW_IP', '0.0.0.0'),
        os.getenv('FAVIEW_PORT', '8080'),
    )
    evwsgi.set_base_module(base)

    for local_path, real_path in MEDIA_PREFIX.iteritems():
        media_dir = ServeStatic(
            settings.MEDIA_ROOT + local_path,
            real_path,
            maxage = 2629000,
        )
        evwsgi.wsgi_cb((
            settings.MEDIA_URL + local_path,
            media_dir,
        ))

    def generic(environ, start_response):
        res = django_handler.handler(environ, start_response)
        return [res]

    evwsgi.wsgi_cb(('', generic))
    evwsgi.set_debug(0)
    evwsgi.run()
Пример #9
0
def start():
	evwsgi.start(db_link_host, str(db_link_port)) 
	evwsgi.set_base_module(base)

	evwsgi.wsgi_cb(('', application))

	evwsgi.set_debug(0)	   
	evwsgi.run()
Пример #10
0
    def start(self):
        self.silence_spurious_logging()

        evwsgi.start(self.interface, self.port)
        evwsgi.set_base_module(base)
        evwsgi.wsgi_cb(("/", wsgi_dispatcher))
        evwsgi.set_debug(0)
        self.logger.info('%r running...' % (self,))
        evwsgi.run()
Пример #11
0
def start():
    evwsgi.start(HOST, str(PORT))
    evwsgi.set_base_module(base)

    evwsgi.wsgi_cb(('/static', views.Staticfile(STATIC_PATH)))
    evwsgi.wsgi_cb(('/broadcast', broadcast))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #12
0
def run_with_fapws3(config):
    import fapws._evwsgi as evwsgi
    from fapws import base, config

    evwsgi.start(config['host'], config['port'])
    evwsgi.set_base_module(base)
    evwsgi.wsgi_cb(('', config['request_handler']))
    evwsgi.set_debug(0)
    evwsgi.run()
Пример #13
0
def start(host, port):
	"""Fapws3 WSGI application"""

	evwsgi.start(host or '127.0.0.1', port or '8000')
	evwsgi.set_base_module(fapws.base)

	evwsgi.wsgi_cb(("/api", web.application.init_memcached()))

	evwsgi.set_debug(0)
	evwsgi.run()
def start(name):
    print "started server at "+str(host)+":"+str(port)
    if name =="meinheld":
        from meinheld import server
        server.set_access_logger(None)
        server.set_error_logger(None)
        server.listen((host, port))
        server.run(app)
    elif name =="gevent":
        #from gevent import wsgi 
        #wsgi.WSGIServer((host, port), application=app.application, log=None).serve_forever() 
        from gevent.pywsgi import WSGIServer
        WSGIServer((host, port), app, log=None).serve_forever()
    elif name =="bjoern":
        import bjoern
        bjoern.listen(app, host, port)
        bjoern.run() 
    elif name =="eventlet":
        import eventlet
        from eventlet import wsgi
        #worker_pool = eventlet.GreenPool(2000)
        #wsgi.server(eventlet.listen(('', port)), app, custom_pool=worker_pool, log=file('/dev/null', 'w'))
        # max_size
        wsgi.server(eventlet.listen(('', port)), app, max_size=10000, log=file('/dev/null', 'w'))
    elif name =="fapws":
        import fapws._evwsgi as evwsgi
        from fapws import base
        evwsgi.start(host, str(port)) 
        evwsgi.set_base_module(base)
        evwsgi.wsgi_cb(('/', app))
        evwsgi.set_debug(0)        
        evwsgi.run()
    elif name=="uwsgi":
        print ("""Enter this command in the console
                \nsudo uwsgi --http :8000 --master --disable-logging --pythonpath /home/a/g --wsgi-file w.py --listen 2  --buffer-size 2048 --async 10000 --ugreen -p 4
            """)
        #  http://osdir.com/ml/python-wsgi-uwsgi-general/2011-02/msg00136.html
        # 
        #  Re: strange SIGPIPE: writing to a closed pipe/socket/fd on image requested from facebook -msg#00136
        """
SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /1 (ip 127.0.0.1) !!!
uwsgi_response_write_body_do(): Broken pipe [core/writer.c line 260]
IOError: write error
        """
        cmd_txt = "sudo uwsgi --http :8000 --master --harakiri 1 --harakiri-verbose --close-on-exec --disable-logging --pythonpath /home/a/todo/test --wsgi-file w2.py --listen 2  --buffer-size 2048 --async 10000 --ugreen -p 4"
        cmd(cmd_txt)
    elif name=="pycgi":
        from wsgiref.handlers import CGIHandler 
        CGIHandler().run(app)
    elif name=="pystandard":
        from wsgiref.simple_server import make_server
        make_server(host, port, app).serve_forever()
Пример #15
0
 def start(self):
     evwsgi.start(self.host, self.port)
     evwsgi.set_base_module(base)
     
     #def app(environ, start_response):
     #    environ['wsgi.multiprocess'] = False
     #    return wsgi_app(environ, start_response)
 
     evwsgi.wsgi_cb(('',self.app))
     #evwsgi.set_debug(1)#开启调试
     evwsgi.set_debug(0)        
     print "libev ABI version:%i.%i" % evwsgi.libev_version()
     evwsgi.run()
Пример #16
0
def start():
    evwsgi.start("0.0.0.0", 8080)
    evwsgi.set_base_module(base)
    
    @trace.Trace()
    def hello(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ["hello world!!"]

    evwsgi.wsgi_cb(("/hello", hello))

    evwsgi.set_debug(0)    
    evwsgi.run()
Пример #17
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(base)

    @trace.Trace()
    def hello(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ["hello world!!"]

    evwsgi.wsgi_cb(("/hello", hello))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #18
0
 def run(self):
     try:
         # since we don't use threads, internal checks are no more required
         sys.setcheckinterval = 100000
         # fapws evwsgi.start() seems to ignore the port
         # if it's a number and requires it to be a string,
         # so we cast it here to get it to work correctly.
         host, port = self.config.bind_addr
         evwsgi.start(host, str(port))
         evwsgi.set_base_module(base)
         evwsgi.wsgi_cb(('', self.config.app))
         evwsgi.set_debug(0)
         evwsgi.run()
     except KeyboardInterrupt:
         self.stop()
Пример #19
0
def start():
    evwsgi.start("0.0.0.0", "8080")

    evwsgi.set_base_module(base)

    def generic(environ, start_response):
        res = django_handler.handler(environ, start_response)
        return [res]

    mediafile = views.Staticfile(django.__path__[0] + "/contrib/admin/media/", maxage=2629000)
    evwsgi.wsgi_cb(("/media/", mediafile))
    evwsgi.wsgi_cb(("", generic))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #20
0
def start():
    handler = Handler(Config({
        'pool_max_runners': 5,
        'provider_appdir': os.path.join(os.path.dirname(__file__), '../../tests'),
        'router_domain': 'example.com'
    }))

    evwsgi.start("0.0.0.0", "8080")

    evwsgi.set_base_module(base)

    evwsgi.wsgi_cb(('', handler))

    evwsgi.set_debug(0)
    evwsgi.run()

    handler.free()
Пример #21
0
def server_http():
    lid = LanguageIdentifier(model)
    headers = [('Content-type', 'text/javascript; charset=utf-8')]
    status = '200 OK'

    def application(environ, start_response):
        params = parse_qs(environ['QUERY_STRING'])
        text = params['q'][0]
        normalize = params['normalize'][0] == 'true'
        pred, conf = lid.classify(text, normalize=normalize)
        start_response(status, headers)
        return [pred + ' ' + str(conf)]

    evwsgi.start('127.0.0.1', '10000')
    evwsgi.set_base_module(base)
    evwsgi.wsgi_cb(('', application))
    evwsgi.set_debug(0)
    evwsgi.run()
Пример #22
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(mybase)

    evwsgi.wsgi_cb(("/env", env))
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/tuplehello", tuplehello))
    evwsgi.wsgi_cb(("/iterhello", iteration))
    evwsgi.wsgi_cb(("/longzipped", staticlongzipped))
    evwsgi.wsgi_cb(("/long", staticlong))
    evwsgi.wsgi_cb(("/elong", embedlong))
    evwsgi.wsgi_cb(("/short", staticshort))
    staticform = views.Staticfile("test.html")
    evwsgi.wsgi_cb(("/staticform", staticform))
    evwsgi.wsgi_cb(("/testpost", testpost))
    evwsgi.wsgi_cb(("/badscript", badscript))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #23
0
def start():
    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(base)
    
 
    evwsgi.wsgi_cb(("/env", env))
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/iterhello", iteration))
    evwsgi.wsgi_cb(("/longzipped", staticlongzipped))
    evwsgi.wsgi_cb(("/long", staticlong))
    evwsgi.wsgi_cb(("/elong", embedlong))
    evwsgi.wsgi_cb(("/short", staticshort))
    staticform=views.Staticfile("test.html")
    evwsgi.wsgi_cb(("/staticform", staticform))
    evwsgi.wsgi_cb(("/testpost", testpost))
    evwsgi.wsgi_cb(("/badscript", badscript))

    evwsgi.set_debug(0)    
    evwsgi.run()
Пример #24
0
def start():
    evwsgi.start("/tmp/hello_unix.sock", "unix")
    evwsgi.set_base_module(base)
    
    def hello(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ["hello world!!"]

    def iteration(environ, start_response):
        start_response('200 OK', [('Content-Type','text/plain')])
        yield "hello"
        yield " "
        yield "world!!"

    
    evwsgi.wsgi_cb(("/hello", hello))
    evwsgi.wsgi_cb(("/iterhello", iteration))

    evwsgi.set_debug(0)    
    evwsgi.run()
Пример #25
0
def start():
    evwsgi.start("0.0.0.0", 8085)

    evwsgi.set_base_module(fapws.base)

    # def generic(environ, start_response):
    #    return ["page not found"]

    def index(environ, start_response):
        print "GET:", environ["fapws.uri"]
        return views.index(environ, start_response)

    def display(environ, start_response):
        print "GET:", environ["fapws.uri"]
        return views.display(environ, start_response)

    def edit(environ, start_response):
        # print environ['fapws.params']
        print "GET:", environ["fapws.uri"]
        r = views.Edit()
        return r(environ, start_response)

    favicon = fapws.contrib.views.Staticfile("static/img/favicon.ico")

    def mystatic(environ, start_response):
        print "GET:", environ["fapws.uri"]
        s = fapws.contrib.views.Staticfile("static/")
        return s(environ, start_response)

    evwsgi.wsgi_cb(("/display/", display))
    evwsgi.wsgi_cb(("/edit", edit))
    evwsgi.wsgi_cb(("/new", edit))
    evwsgi.wsgi_cb(("/static/", mystatic))
    # evwsgi.wsgi_cb(('/favicon.ico',favicon)),

    evwsgi.wsgi_cb(("/", index))
    # evhttp.gen_http_cb(generic)
    evwsgi.set_debug(0)
    print "libev ABI version:%i.%i" % evwsgi.libev_version()
    evwsgi.run()
Пример #26
0
def start():
    evwsgi.start("0.0.0.0", "8085")

    evwsgi.set_base_module(fapws.base)

    #def generic(environ, start_response):
    #    return ["page not found"]

    def index(environ, start_response):
        print "GET:", environ['fapws.uri']
        return views.index(environ, start_response)

    def display(environ, start_response):
        print "GET:", environ['fapws.uri']
        return views.display(environ, start_response)

    def edit(environ, start_response):
        #print environ['fapws.params']
        print "GET:", environ['fapws.uri']
        r = views.Edit()
        return r(environ, start_response)

    favicon = fapws.contrib.views.Staticfile("static/img/favicon.ico")

    def mystatic(environ, start_response):
        print "GET:", environ['fapws.uri']
        s = fapws.contrib.views.Staticfile("static/")
        return s(environ, start_response)

    evwsgi.wsgi_cb(("/display/", display))
    evwsgi.wsgi_cb(("/edit", edit))
    evwsgi.wsgi_cb(("/new", edit))
    evwsgi.wsgi_cb(("/static/", mystatic))
    #evwsgi.wsgi_cb(('/favicon.ico',favicon)),

    evwsgi.wsgi_cb(("/", index))
    #evhttp.gen_http_cb(generic)
    evwsgi.set_debug(0)
    print "libev ABI version:%i.%i" % evwsgi.libev_version()
    evwsgi.run()
Пример #27
0
def start():
    import os
    port = int(os.environ.get('PORT', 9999))
    evwsgi.start('0.0.0.0', port)
    evwsgi.set_base_module(base)

    def html_message(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    def short_message(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    def average_message(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    def long_message(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    def json_message(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    def slow_request(environ, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return ['hello world!!']

    evwsgi.wsgi_cb(('/', html_message))
    evwsgi.wsgi_cb(('/short-message', short_message))
    evwsgi.wsgi_cb(('/average-message', average_message))
    evwsgi.wsgi_cb(('/long-message', long_message))
    evwsgi.wsgi_cb(('/json-message', json_message))
    evwsgi.wsgi_cb(('/slow-request', slow_request))

    evwsgi.set_debug(0)
    evwsgi.run()
Пример #28
0
# -*- coding: utf-8 -*-

import time


def application(environ, start_response):
    status = '404 Not Found'
    output = 'Pong!'

    response_headers = [('Content-type', 'text/plain')]
    #response_headers = [('Content-type', 'text/plain'),
    #                    ('Content-Length', str(len(output)))]

    start_response(status, response_headers)
    # return [output] # <- works OK
    yield output # <- does not convey 404 status to client
    time.sleep(5)
    yield "and"
    time.sleep(5)
    yield "Ping!!!"

if __name__=="__main__":
    import fapws._evwsgi as evwsgi
    from fapws import base

    evwsgi.start("0.0.0.0", "8080")
    evwsgi.set_base_module(base)
    evwsgi.wsgi_cb(("/", application))
    evwsgi.set_debug(1)
    evwsgi.run()
Пример #29
0
                                request_queue_size=40)
            try:
                server.start()
            except KeyboardInterrupt:
                server.stop()
        elif sname == "gevent":
            from gevent.wsgi import WSGIServer
            server = WSGIServer(("0.0.0.0", port), wsgifunc, log=None)
            server.serve_forever()
        elif sname == "fapws3":
            import fapws._evwsgi as evwsgi
            from fapws import base
            evwsgi.start("0.0.0.0", str(port))
            evwsgi.set_base_module(base)
            evwsgi.wsgi_cb(("", wsgifunc))
            evwsgi.set_debug(0)
            evwsgi.run()
        else:
            usage()
    elif len(argv) == 0:
        from flup.server.fcgi import WSGIServer
        server = WSGIServer(wsgifunc,
                            bindAddress=None,
                            multiplexed=False,
                            multithreaded=False)
        server.run()
        #web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
        #app.run()
    else:
        usage()
Пример #30
0
def serv(queue):
    evwsgi.start("127.0.0.1", "8080")
    evwsgi.set_base_module(base)
    evwsgi.wsgi_cb(("/", application))
    evwsgi.set_debug(0)
    evwsgi.run()
Пример #31
0
    return s(environ, start_response)


@zip.Gzip()
def css_static(environ, start_response):
    s = views.Staticfile("static/css/", maxage=(3 * 60))  # 3 minutes
    return s(environ, start_response)


@zip.Gzip()
def js_static(environ, start_response):
    s = views.Staticfile("static/js/", maxage=(3 * 60))  # 3 minutes
    return s(environ, start_response)


evwsgi.wsgi_cb(("/static/css/", css_static))
evwsgi.wsgi_cb(("/static/js/", js_static))
evwsgi.wsgi_cb(("/static/", general_static))

# Django handler
@zip.Gzip()
def generic(environ, start_response):
    res = django_handler.handler(environ, start_response)
    return [res]


evwsgi.wsgi_cb(("", generic))

evwsgi.set_debug(0)
evwsgi.run()
Пример #32
0
    host='127.0.0.1',
    settings='settings',
)

parser.add_option('--port', dest='port')
parser.add_option('--host', dest='host')
parser.add_option('--settings', dest='settings')
parser.add_option('--pythonpath', dest='pythonpath')

options, args = parser.parse_args()
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings

sys.setcheckinterval = 100000  # since we don't use threads, internal checks are no more required

if options.pythonpath:
    sys.path.insert(0, options.pythonpath)

print 'start on', (options.host, options.port)
evwsgi.start(options.host, options.port)
evwsgi.set_base_module(base)


def generic(environ, start_response):
    res = django_handler.handler(environ, start_response)
    return [res]


evwsgi.wsgi_cb(('', generic))
evwsgi.set_debug(True)
evwsgi.run()
Пример #33
0
def main():
    global identifier

    parser = optparse.OptionParser()
    parser.add_option('-s',
                      '--serve',
                      action='store_true',
                      default=False,
                      dest='serve',
                      help='launch web service')
    parser.add_option('--host',
                      default=HOST,
                      dest='host',
                      help='host/ip to bind to')
    parser.add_option('--port',
                      default=PORT,
                      dest='port',
                      help='port to listen on')
    parser.add_option('-v',
                      action='count',
                      dest='verbosity',
                      help='increase verbosity (repeat for greater effect)')
    parser.add_option('-m', dest='model', help='load model from file')
    parser.add_option(
        '-l',
        '--langs',
        dest='langs',
        help='comma-separated set of target ISO639 language codes (e.g en,de)')
    parser.add_option('-r',
                      '--remote',
                      action="store_true",
                      default=False,
                      help='auto-detect IP address for remote access')
    parser.add_option('-b',
                      '--batch',
                      action="store_true",
                      default=False,
                      help='specify a list of files on the command line')
    parser.add_option('--demo',
                      action="store_true",
                      default=False,
                      help='launch an in-browser demo application')
    parser.add_option('-d',
                      '--dist',
                      action='store_true',
                      default=False,
                      help='show full distribution over languages')
    parser.add_option('-u', '--url', help='langid of URL')
    parser.add_option(
        '--line',
        action="store_true",
        default=False,
        help='process pipes line-by-line rather than as a document')
    parser.add_option('-n',
                      '--normalize',
                      action='store_true',
                      default=False,
                      help='normalize confidence scores to probability values')
    options, args = parser.parse_args()

    if options.verbosity:
        logging.basicConfig(level=max((5 - options.verbosity) * 10, 0))
    else:
        logging.basicConfig()

    if options.batch and options.serve:
        parser.error("cannot specify both batch and serve at the same time")

    # unpack a model
    if options.model:
        try:
            identifier = LanguageIdentifier.from_modelpath(
                options.model, norm_probs=options.normalize)
            logger.info("Using external model: %s", options.model)
        except IOError as e:
            logger.warning("Failed to load %s: %s" % (options.model, e))

    if identifier is None:
        identifier = LanguageIdentifier.from_modelstring(
            model, norm_probs=options.normalize)
        logger.info("Using internal model")

    if options.langs:
        langs = options.langs.split(",")
        identifier.set_languages(langs)

    def _process(text):
        """
    Set up a local function to do output, configured according to our settings.
    """
        if options.dist:
            payload = identifier.rank(text)
        else:
            payload = identifier.classify(text)

        return payload

    if options.url:
        import contextlib
        import urllib.request, urllib.error, urllib.parse
        try:
            from urllib.request import urlopen
        except ImportError:
            from urllib2 import urlopen
        with contextlib.closing(urlopen(options.url)) as url:
            text = url.read()
            output = _process(text)
            print(options.url, len(text), output)

    elif options.serve or options.demo:
        # from http://stackoverflow.com/questions/166506/finding-local-ip-addresses-in-python
        if options.remote and options.host is None:
            # resolve the external ip address
            import socket
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("google.com", 80))
            hostname = s.getsockname()[0]
        elif options.host is None:
            # resolve the local hostname
            import socket
            hostname = socket.gethostbyname(socket.gethostname())
        else:
            hostname = options.host

        if options.demo:
            import webbrowser
            webbrowser.open('http://{0}:{1}/demo'.format(
                hostname, options.port))
        try:
            if FORCE_WSGIREF: raise ImportError
            # Use fapws3 if available
            import fapws._evwsgi as evwsgi
            from fapws import base
            evwsgi.start(hostname, str(options.port))
            evwsgi.set_base_module(base)
            evwsgi.wsgi_cb(('', application))
            evwsgi.set_debug(0)
            evwsgi.run()
        except ImportError:
            print("Listening on %s:%d" % (hostname, int(options.port)))
            print("Press Ctrl+C to exit")
            httpd = make_server(hostname, int(options.port), application)
            try:
                httpd.serve_forever()
            except KeyboardInterrupt:
                pass
    elif options.batch:
        # Start in batch mode - interpret input as paths rather than content
        # to classify.
        import sys, os, csv
        import multiprocessing as mp

        def generate_paths():
            for line in sys.stdin:
                path = line.strip()
                if path:
                    if os.path.isfile(path):
                        yield path
                    else:
                        # No such path
                        pass

        writer = csv.writer(sys.stdout)
        pool = mp.Pool()
        if options.dist:
            writer.writerow(['path'] + nb_classes)
            for path, ranking in pool.imap_unordered(rank_path,
                                                     generate_paths()):
                ranking = dict(ranking)
                row = [path] + [ranking[c] for c in nb_classes]
                writer.writerow(row)
        else:
            for path, (lang,
                       conf) in pool.imap_unordered(cl_path, generate_paths()):
                writer.writerow((path, lang, conf))
    else:
        import sys
        if sys.stdin.isatty():
            # Interactive mode
            while True:
                try:
                    print(">>>", end=' ')
                    text = input()
                except Exception as e:
                    print(e)
                    break
                print(_process(text))
        else:
            # Redirected
            if options.line:
                for line in sys.stdin:
                    print(_process(line))
            else:
                print(_process(sys.stdin.read()))
Пример #34
0
parser.set_defaults(
    port='8000',
    host='127.0.0.1',
    settings='settings',
)

parser.add_option('--port', dest='port')
parser.add_option('--host', dest='host')
parser.add_option('--settings', dest='settings')
parser.add_option('--pythonpath', dest='pythonpath')

options, args = parser.parse_args()
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings

sys.setcheckinterval=100000 # since we don't use threads, internal checks are no more required

if options.pythonpath:
    sys.path.insert(0, options.pythonpath)

print 'start on', (options.host, options.port)
evwsgi.start(options.host, options.port)
evwsgi.set_base_module(base)

def generic(environ, start_response):
    res=django_handler.handler(environ, start_response)
    return [res]

evwsgi.wsgi_cb(('',generic))
evwsgi.set_debug(True)
evwsgi.run()
Пример #35
0
def main():

    parser = optparse.OptionParser()
    parser.add_option('-s', '--serve',
                      action='store_true', default=False, dest='serve', help='launch web service')
    parser.add_option('--host',
                      default=HOST, dest='host', help='host/ip to bind to')
    parser.add_option('--port',
                      default=PORT, dest='port', help='port to listen on')
    parser.add_option('-v',
                      action='count', dest='verbosity', help='increase verbosity (repeat for greater effect)')
    parser.add_option('-m',
                      dest='model', help='load model from file')
    parser.add_option('-l', '--langs',
                      dest='langs', help='comma-separated set of target ISO639 language codes (e.g en,de)')
    parser.add_option('-r', '--remote',
                      action="store_true", default=False, help='auto-detect IP address for remote access')
    parser.add_option('-b', '--batch',
                      action="store_true", default=False, help='specify a list of files on the command line')
    parser.add_option('--demo',
                      action="store_true", default=False, help='launch an in-browser demo application')
    parser.add_option('-d', '--dist',
                      action='store_true', default=False, help='show full distribution over languages')
    parser.add_option('-u', '--url',
                      help='langid of URL')
    parser.add_option('--line',
                      action="store_true", default=False, help='process pipes line-by-line rather than as a document')
    parser.add_option('-n', '--normalize',
                      action='store_true', default=False, help='normalize confidence scores to probability values')
    options, args = parser.parse_args()

    if options.verbosity:
        logging.basicConfig(level=max((5-options.verbosity)*10, 0))
    else:
        logging.basicConfig()

    if options.batch and options.serve:
        parser.error("cannot specify both batch and serve at the same time")

    # unpack a model
    identifier = None
    if options.model:
        try:
            identifier = LanguageIdentifier.from_modelpath(
                options.model, norm_probs=options.normalize)
            logger.info("Using external model: %s", options.model)
        except IOError as e:
            logger.warning("Failed to load %s: %s" % (options.model,e))

    if identifier is None:
        identifier = LanguageIdentifier.from_model(norm_probs=options.normalize)
        logger.info("Using internal model")

    if options.langs:
        langs = options.langs.split(",")
        identifier.set_languages(langs)

    def _process(text):
        """
        Set up a local function to do output, configured according to our settings.
        """
        if options.dist:
            payload = identifier.rank(text)
        else:
            payload = identifier.classify(text)

        return payload


    if options.url:
        import urllib.request, urllib.error, urllib.parse
        import contextlib
        with contextlib.closing(urllib.request.urlopen(options.url)) as url:
            text = url.read()
            output = _process(text)
            print(options.url, len(text), output)

    elif options.serve or options.demo:
        # from http://stackoverflow.com/questions/166506/finding-local-ip-addresses-in-python
        if options.remote and options.host is None:
            # resolve the external ip address
            import socket
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("google.com", 80))
            hostname = s.getsockname()[0]
        elif options.host is None:
            # resolve the local hostname
            import socket
            hostname = socket.gethostbyname(socket.gethostname())
        else:
            hostname = options.host

        if options.demo:
            import webbrowser
            webbrowser.open('http://{0}:{1}/demo'.format(hostname, options.port))
        try:
            if FORCE_WSGIREF:
                raise ImportError
            # Use fapws3 if available
            import fapws._evwsgi as evwsgi
            from fapws import base
            evwsgi.start(hostname, str(options.port))
            evwsgi.set_base_module(base)
            evwsgi.wsgi_cb(('', application))
            evwsgi.set_debug(0)
            evwsgi.run()
        except ImportError:
            print("Listening on %s:%d" % (hostname, int(options.port)))
            print("Press Ctrl+C to exit")
            from wsgiref.simple_server import make_server
            httpd = make_server(hostname, int(options.port), application)
            try:
                httpd.serve_forever()
            except KeyboardInterrupt:
                pass
    elif options.batch:
        # Start in batch mode - interpret input as paths rather than content
        # to classify.
        import sys
        import os
        import csv
        import multiprocessing as mp

        def generate_paths():
            for line in sys.stdin:
                path = line.strip()
                if path:
                    if os.path.isfile(path):
                        yield path
                    else:
                        # No such path
                        pass

        writer = csv.writer(sys.stdout)
        pool = mp.Pool()
        if options.dist:
            writer.writerow(['path']+nb_classes)
            for path, ranking in pool.imap_unordered(rank_path, generate_paths()):
                ranking = dict(ranking)
                row = [path] + [ranking[c] for c in nb_classes]
                writer.writerow(row)
        else:
            for path, (lang,conf) in pool.imap_unordered(cl_path, generate_paths()):
                writer.writerow((path, lang, conf))
    else:
        import sys
        if sys.stdin.isatty():
            # Interactive mode
            while True:
                try:
                    print(">>>", end=' ')
                    text = input()
                except Exception:
                    break
                print(_process(text))
        else:
            # Redirected
            if options.line:
                for line in sys.stdin.readlines():
                    print(_process(line))
            else:
                print(_process(sys.stdin.read()))