Exemple #1
0
def main():
    parser = argparse.ArgumentParser(description='Process server info')
    parser.add_argument('-A', default='image', choices=['image', 'altdemo', 'myapp',
                        'quotes', 'chat', 'cookie'], type=str)
    parser.add_argument('-p', type=int, choices=range(8000,10000),
                        default=random.randint(8000,9999))
    args = parser.parse_args()
    args = vars(args)

    if args['A'] == 'image':
        imageapp.setup()
        x = imageapp.create_publisher()

    s = socket.socket()         # Create a socket object
    host = socket.getfqdn()     # Get local machine name
    port = args['p']
    s.bind((host, port))        # Bind to the port
    args['host'] = host

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5)                 # Now wait for client connection.

    print 'Entering infinite loop; hit CTRL-C to exit'
    while True:
        # Establish connection with client.
        c, (client_host, client_port) = s.accept()
        print 'Got connection from', client_host, client_port

        try:
            handle_connection(c, args)
        finally:
            imageapp.teardown()
Exemple #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-A', '--app', 
            help='specify type of app (altdemo, myapp, image)')
    parser.add_argument('-p', '--port', help='specify port', type=int)

    args = parser.parse_args()

    app_type = ''
    if args.app == 'altdemoapp':
        print 'running altdemoapp...'
        app_type = "altdemoapp" 
    elif args.app == 'myapp':
        print 'running myapp...'
        app_type = "myapp"
    elif args.app == 'imageapp':
        print 'running imageapp...'
        app_type = "imageapp"
    elif args.app == 'quotes':
        print 'running quotes...'
        app_type = "quotes"
    elif args.app == 'chat':
        print 'running chat...'
        app_type = "chat"
    elif args.app == 'cookie':
        print 'running cookie...'
        app_type = "cookie"
    else:
        print '** Error: argument must be "imageapp", "myapp", "altdemoapp", "quotes", "chat", or "cookie"'
        return

    port = 0;
    if args.port:
        port = args.port
    else:
        port = random.randint(8000, 9999)

    s = socket.socket()         # Create a socket object
    host = socket.getfqdn()     # Get local machine name
    # host = "localhost"
    s.bind((host, port))        # Bind to the port

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5)                 # Now wait for client connection.

    print 'Entering infinite loop; hit CTRL-C to exit'
    while True:
        # Establish connection with client.    
        c, (client_host, client_port) = s.accept()
        
        print 'Got connection from', client_host, client_port
        handle_connection(c, app_type)

    imageapp.teardown()
Exemple #3
0
def main(socketmodule=None):
    if socketmodule is None:
        socketmodule = socket

    app, port = get_args()

    if app == 'myapp':
        s = socketmodule.socket()
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        s.bind((host, port))
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        s.listen(5)
        print 'Entering infinite loop; hit CTRL-C to exit'
        while True:
            c, (client_host, client_port) = s.accept()
            print 'Got connection from', client_host, client_port
            handle_connection(c, client_port)

    elif app == 'image':
        imageapp.setup()
        p = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
        from wsgiref.simple_server import make_server
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        httpd = make_server('', port, wsgi_app)
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        try:
            httpd.serve_forever()
        finally:
            imageapp.teardown()

    elif app == 'altdemo':
        p = create_publisher()
        wsgi_app = quixote.get_wsgi_app()
        from wsgiref.simple_server import make_server
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        p.is_thread_safe = True
        httpd = make_server('', port, wsgi_app)
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        httpd.serve_forever()

    elif app in ('quotes', 'chat'):
        if port == 0:
            port = random.randint(8000, 9999)
        os.chdir(app)
        os.system("python2.7 %s-server %d" % (app, port))
Exemple #4
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument( '-A', '--runApp', help = 'Application to run (image/altdemo/myapp)' )
    parser.add_argument( '-p', '--portNumb', help = 'Specified port number', type=int )
    
    args = parser.parse_args()

    # Handle port input (if there)
    if args.portNumb:
        port = args.portNumb
    else:
        port = random.randint(8000, 9999)

	# Determine what app to create
    if args.runApp == 'myapp':
	    wsgi_app = make_app()
    elif args.runApp == 'image':
        imageapp.setup()
        p        = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
    elif args.runApp == 'altdemo':
    	p        = create_publisher()
    	wsgi_app = quixote.get_wsgi_app()
    elif args.runApp == 'quotes':
    	import quotes
    	wsgi_app = quotes.setup()
    elif args.runApp == 'chat':
    	import chat
    	wsgi_app = chat.setup()
    elif args.runApp == 'cookie':
    	import cookieapp
        wsgi_app = cookieapp.wsgi_app
    else:
		print 'Invalid Application...'
		return
    	
    s = socket.socket()         # Create a socket object
    host = socket.getfqdn()     # Get local machine name
    s.bind((host, port))        # Bind to the port

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5)                 # Now wait for client connection.

    print 'Entering infinite loop; hit CTRL-C to exit'
    while True:
        # Establish connection with client.
        c, (client_host, client_port) = s.accept()
        print 'Got connection from', client_host, client_port
        try:
            handle_connection(c, port, wsgi_app)
        finally:
            imageapp.teardown()
Exemple #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-A", "--app", help="please specify app", required=True)
    parser.add_argument("-p", "--port", type=int, help="please specify port")
    args = parser.parse_args()
    app = ''

    if args.port:
        port = args.port
        print args.port
    else:
        port = random.randint(8000, 9999)

    if args.app:
        app = args.app

    if args.app == "image":
        imageapp.setup()
        p = imageapp.create_publisher()

    s = socket.socket()         # Create a socket object
    host = socket.getfqdn() # Get local machine name
    s.bind((host, port))        # Bind to the port

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5)                 # Now wait for client connection.

    print 'Entering infinite loop; hit CTRL-C to exit'
    while True:
        print 'Waiting for a connection.'
        # Establish connection with client.
        c, (client_host, client_port) = s.accept()
        print 'Got connection from', client_host, client_port
        try:
            t = threading.Thread(target=handle_connection,
                args=(c, port, app))
            t.start()
            print t.getName()
        finally:
            imageapp.teardown()
import socket
import random

### here is the code needed to create a WSGI application interface to
### a Quixote app:

import quixote
import imageapp

imageapp.setup()

p = imageapp.create_publisher()
wsgi_app = quixote.get_wsgi_app()

### now that we have a WSGI app, we can run it in the WSGI reference server:

from wsgiref.simple_server import make_server

host = socket.getfqdn() # Get local machine name
#port = random.randint(8000, 9999)
port = 8000
httpd = make_server('', port, wsgi_app)
print "Serving at http://%s:%d/..." % (host, port,)

try:
    httpd.serve_forever()
finally:
    imageapp.teardown()
Exemple #7
0
def main(socketmodule=None):
    if socketmodule is None:
        socketmodule = socket

    app, port = get_args()

    if app == 'myapp':
        s = socketmodule.socket()
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        s.bind((host, port))
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        s.listen(5)
        print 'Entering infinite loop; hit CTRL-C to exit'
        while True:
            c, (client_host, client_port) = s.accept()
            print 'Got connection from', client_host, client_port
            handle_connection(c, client_port)

    elif app == 'image':
        imageapp.setup()
        p = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
        from wsgiref.simple_server import make_server
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        httpd = make_server('', port, wsgi_app)
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        try:
            httpd.serve_forever()
        finally:
            imageapp.teardown()       

    elif app == 'altdemo':
        p = create_publisher()
        wsgi_app = quixote.get_wsgi_app()
        from wsgiref.simple_server import make_server
        host = socketmodule.getfqdn()
        if port == 0:
            port = random.randint(8000, 9999)
        p.is_thread_safe = True
        httpd = make_server('', port, wsgi_app)
        print 'Starting server on', host, port
        print 'The Web server URL for this would be http://%s:%d/' % (host, port)
        httpd.serve_forever()

    elif app == 'quotes':
        if port == 0:
             port = random.randint(8000, 9999)
        quotes_app = QuotesApp('quotes.txt', './quoteshtml')
        Server(port, quotes_app).serve_forever()

    elif app == 'chat':
        if port == 0:
            port = random.randint(8000, 9999)
        chat_app = ChatApp('./chathtml')
        Server(port, chat_app).serve_forever()

    elif app == 'cookie':
        if port == 0:
            port = random.randint(8000, 9999)
        import cookieapp
        from wsgiref.simple_server import make_server
        the_wsgi_app = cookieapp.wsgi_app
        host = socket.getfqdn() # Get local machine name
        httpd = make_server('', port, the_wsgi_app)
        print "Serving at http://%s:%d/..." % (host, port,)
        httpd.serve_forever()
def main():
    """
    Initializes the Server.

    Parses command line arguments (if present),
    then initializes the wsgi_app and starts up the server,
    then waits for connections
    """

    apps = [
        'fires', 'hw6',
        'imageapp',
        'quixote_demo',
        'quotes',
        'chat',
        'cookie'
    ]
    parser = argparse.ArgumentParser(
        description='A WSGI Server implemented for CSE491-001.',
        epilog='Please check the non-existent documentation for more info.',
        formatter_class=argparse.RawTextHelpFormatter
    )
    # Add the '-?' alias for '--help', which I prefer to use:
    parser.add_argument('-?',
        action='help',
        help='Alias for --help')
    # Add the application argument:
    parser.add_argument('--app',
        nargs='?',
        dest='app',
        default='fires',
        choices=apps,
        help='\n'.join([
            'Which WSGI application to run.',
            '(default: "%(default)s" - my homework 6)',
            'Alias: -A'
            ]))
    parser.add_argument('-A',
        nargs='?',
        dest='app',
        default='fires',
        choices=apps,
        help=argparse.SUPPRESS)
    # Add the port argument:
    parser.add_argument('--port',
        nargs='?',
        default=random.randint(8000, 9999),
        type=int,
        help='\n'.join([
            'Which port to start the server on.',
            '(default: random integer between 8000 and 9999)',
            'Alias: -p'
            ]))
    # After that, parse the command-line arguments.
    args = parser.parse_args()

    # Create a socket object
    sock = socket.socket()
    # Get local machine name
    host = socket.getfqdn()

    if host in ('magrathea', 'Thoth'):
        # For testing, I don't want to have to change my url all the damn time.
        port = 8080
    else:
        port = args.port
    # Bind to the port
    # TODO figure out how to immediately unbind when I'm done
    sock.bind((host, port))
    print 'Starting server at http://%s:%d/' % (host, port)
    # Now wait for client connection.
    sock.listen(5)

    # get this from commandline
    app_to_run = args.app
    if app_to_run == 'quixote_demo':
        # quixote stuff for testing with that
        p = create_publisher()
        # p.is_thread_safe = True # hack...
        wsgi_app = quixote.get_wsgi_app()
    elif app_to_run == 'imageapp':
        imageapp.setup()
        p = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
    elif app_to_run == 'quotes':
        wsgi_app = QuotesApp('./quotes/quotes.txt', './quotes/html')
    elif app_to_run == 'chat':
        wsgi_app = ChatApp('./chat/html')
    elif app_to_run == 'cookie':
        wsgi_app = cookieapp.wsgi_app
    else: #if app_to_run == 'fires': # default
        wsgi_app = app.make_app()


    print 'Entering infinite loop; hit CTRL-C to exit'
    try:
        while True:
            # Establish connection with client.
            conn, (client_host, client_port) = sock.accept()
            print 'Got connection from', client_host, client_port
            handle_connection(conn, wsgi_app)
    finally:
        # teardown stuffs
        if app_to_run == 'imageapp':
            imageapp.teardown()
        sock.shutdown(2)
        sock.close()