Giter Site home page Giter Site logo

webserver's Introduction

Python WebServer

A EPOLL event driven web server, it's not enough completed for production, just for study.

Hello World Example

import argparse
from webserver import *


class IndexHandler(RequestHandler):

    def get(self):
        self.write('<h1>Hello, world!</h1>')


def main(unix_sock, bind, port):
    application = Application([
        ('/', IndexHandler)
    ])
    http_server = TCPServer(application, unix_sock, bind, port)
    http_server.start()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--unix_sock', default=None,
                        help='Run as unix socket')
    parser.add_argument('--bind', '-b', default='',
                        help='Specify alternate bind address ')
    parser.add_argument('--port', '-p', default=8080,
                        help='Specify alternate port [default: 8080]')
    args = parser.parse_args()
    main(unix_sock=args.unix_sock, bind=args.bind, port=args.port)

Start

$: python3 app.py -p 8888 OR $: python3 app.py --unix_sock /tmp/test.sock

Application

实例化Application加载handler,并传入TCPServer

application = Application([
    ('/', IndexHandler)
])
http_server = TCPServer(application, unix_sock, bind, port)

处理请求时会在Application的_get_host_handlers方法里匹配handler

def _get_host_handlers(self, request):
    """match the handler."""
    if request.uri:
        uri = request.uri.lower().rstrip('/')
    else:
        return None
    if uri == '': uri = '/'
    for pattern, handler in self.handlers:
        if pattern == uri:
            return handler
    return None

TCP Server接受请求后执行Application.__call__

class TCPServer(object):

    ...

    def _handle_request(self, fileno):
        ...
        self.responses[fileno] = self.application(self.request)
        ...

...

class Application(object):

    ...

    def __call__(self, request):
        handler = self._get_host_handlers(request)
        ...

最后执行RequestHandler的工厂函数

class RequestHandler(object):

    ...

    def _execute(self, fileno):
        ...
        getattr(self, self.request.method.lower())()
        ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.