Package: x100http
NAMEx100http, web framework support customing file upload processingSYNOPSISfrom x100http import X100HTTP

app = X100HTTP()

def hello_world(request):
    remote_ip = request.get_remote_ip()
    response = "<html><body>hello, " + remote_ip + "</body></html>"
    return response

app.get("/", hello_world)
app.run("0.0.0.0", 8080)DESCRIPTIONx100http is a lite webframework designed for processing HTTP file upload.CLASS X100HTTPX100HTTP()return a instance of x100http which wrapped below functions.run(listern_ip, listen_port)run a forking server on addresslistern_ip:listern_portget(url, handler_function)set a route acl of HTTP “GET” method.handler_functionwill be called whenurlbe visited.handler_functionmust return a string as the HTTP response body to the visitor.structrequest(will explain below) will be passed to the handlder function when it is called.post(url, handler_function)set a route acl of HTTP “POST” method with header “Content-Type: application/x-www-form-urlencoded”.handler_functionwill be called when HTTP client submit a form with the actionurl.handler_functionmust return a string as the HTTP response body to the visitor.structrequest(will explain below) will be passed to the handlder function when it is called.static(url_prefix, file_path, cors=allow_domain)set a route acl for static fileStatic file request withurl_prefixwill be routing to the file infile_path.Default value of cors is “*”, allow all CORS request matching this route rule.upload(url, upload_handler_class)set a route acl of HTTP “POST” method with header “Content-Type: multipart/form-data”.A new instance of classupload_handler_classwill be created when file upload start.struct “request” (will explain below) will be passed toupload_handler_class.upload_start().upload_handler_class.upload_process()will be called every time when the buffer is full when file uploading.two args will be passed toupload_handler_class.upload_process().first arg is the name of the input in the form, second arg is the content of the input in the form.the binary content of the upload file will be passed by the second arg.struct “request” (will explain below) will NOT be passed toupload_handler_class.upload_finish().upload_handler_class.upload_finish()will be called when file upload finished, this function must return a string as the HTTP response body to the visitor.struct “request” (will explain below) will be passed toupload_handler_class.upload_finish().set_upload_buf_size(buf_size)set the buffer size of the stream reader while file uploading.the unit ofbuf_sizeis byte, default value is 4096 byte.upload_handler_class.upload_process()will be called to process the buffer every time when the buffer is full.ROUTINGx100http route accept a url and a function/class/path.There are three four of routes - get, post, static and upload.app.get("/get_imple", get_simple)
app.post("/post_simple", post_simple)
app.upload("/upload_simple", UploadClass)
app.static("/static/test/", "/tmp/sta/")routing for HTTP GET can be more flexible like this:app.get("/one_dir/<arg_first>_<arg_second>.py?abc=def", regex_get)allow all domain for CORS like this:app.static("/static/test/", "/tmp/sta/", cors="*")CLASS X100REQUESTA instance of classX100Requestwill be passed into every handler function.get_remote_ip()Return the IP address of the visitor.get_body()Return the body section of the HTTP request.Will be empty when the HTTP method is “GET” or “POST - multipart/form-data”.get_query_string()Return the query string of the page was accessed, if any.get_arg(arg_name)args parsed fromquery_stringwhen the request is sent by “GET” or “POST - multipart/form-data”.args parsed frombodywhen the request is sent by “POST - application/x-www-form-urlencoded”.get_header(header_name)Return the header`s value of theheader_name, if any.CLASS X100RESPONSEset_body(content)Set the response data to visitor.Type ‘str’ and type ‘bytes’ are both accepted.set_header(name, value)Set the HTTP header.HTTP ERROR 500visitor will get HTTP error “500” when the handler function of the url he visit raise an error or code something wrong.SUPPORTED PYTHON VERSIONSx100http only supports python 3.4 or newer, because ofre.fullmatchandos.sendfile.EXAMPLESget visitor ipfrom x100http import X100HTTP

app = X100HTTP()

def hello_world(request):
    remote_ip = request.get_remote_ip()
    response = "<html><body>hello, " + remote_ip + "</body></html>"
    return response

app.get("/", hello_world)
app.run("0.0.0.0", 8080)post method routefrom x100http import X100HTTP

app = X100HTTP()

def index(request):
    response = "<html><body>" \
        + "<form name="abc" action="/form" method="post">" \
        + "<input type="text" name="abc" />" \
        + "<input type="submit" name="submit" />" \
        + "</form>" \
        + "</body></html>"
    return response

def post_handler(request):
    remote_ip = request.get_remote_ip()
    abc = request.get_arg('abc')
    response = "hello, " + remote_ip + " you typed: " + abc
    return response

app.get("/", index)
app.post("/form", post_handler)
app.run("0.0.0.0", 8080)process file uploadfrom x100http import X100HTTP, X100Response

class UploadHandler:

    def upload_start(self, request):
        self.content = "start"

    def upload_process(self, key, line):
        self.content += line.decode()

    def upload_finish(self, request):
        return "upload succ, content = " + self.content

app = X100HTTP()
app.upload("/upload", UploadHandler)
app.run("0.0.0.0", 8080)set http headerfrom x100http import X100HTTP, X100Response

def get_custom_header(request):
    remote_ip = request.get_remote_ip()
    response = X100Response()
    response.set_header("X-My-Header", "My-Value")
    response.set_body("<html><body>hello, " + remote_ip + "</body></html>")
    return response

app = X100HTTP()
app.upload("/", get_custom_header)
app.run("0.0.0.0", 8080)more flexible routingfrom x100http import X100HTTP

def regex_get(request):
    first = request.get_arg("arg_first")
    second = request.get_arg("arg_second")
    abc = request.get_arg("abc")
    return "hello, " + first + second + abc

app = X100HTTP()
app.get("/one_dir/<arg_first>_<arg_second>.py?abc=def", regex_get)
app.run("0.0.0.0", 8080)