web框架支持自定义文件上传处理

x100http的Python项目详细描述



名称

x100http, web framework support customing file upload processing

概要

from 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)

说明

x100http is a lite webframework designed for processing HTTP file upload.

X100http类

x100http()

return a instance of x100http which wrapped below functions.

运行(listern ip,listen port)

run a forking server on address ^{tt1}$:^{tt2}$

get(url,handler_函数)

set a route acl of HTTP “GET” method.

^{tt3}$ will be called when ^{tt4}$ be visited.

^{tt3}$ must return a string as the HTTP response body to the visitor.

struct ^{tt6}$ (will explain below) will be passed to the handlder function when it is called.

post(url,handler_函数)

set a route acl of HTTP “POST” method with header “Content-Type: application/x-www-form-urlencoded”.

^{tt3}$ will be called when HTTP client submit a form with the action ^{tt4}$.

^{tt3}$ must return a string as the HTTP response body to the visitor.

struct ^{tt6}$ (will explain below) will be passed to the handlder function when it is called.

静态(URL前缀,文件路径,CORS=允许域)

set a route acl for static file

Static file request with ^{tt11}$ will be routing to the file in ^{tt12}$.

Default value of cors is “*”, allow all CORS request matching this route rule.

上传(url,上传处理程序类)

set a route acl of HTTP “POST” method with header “Content-Type: multipart/form-data”.

A new instance of class ^{tt13}$ will be created when file upload start.

struct “request” (will explain below) will be passed to ^{tt14}$.

^{tt15}$ will be called every time when the buffer is full when file uploading.

two args will be passed to ^{tt15}$.

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 to ^{tt17}$.

^{tt17}$ 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 to ^{tt17}$.

设置上传buf大小(buf大小)

set the buffer size of the stream reader while file uploading.

the unit of ^{tt20}$ is byte, default value is 4096 byte.

^{tt15}$ will be called to process the buffer every time when the buffer is full.


布线

x100http 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/")

HTTP GET路由可以更加灵活:

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

允许像这样的cors的所有域:

app.static("/static/test/", "/tmp/sta/", cors="*")

X100级请求

A instance of class ^{tt22}$ will be passed into every handler function.

获取远程IP()

Return the IP address of the visitor.

获取身体()

Return the body section of the HTTP request.

Will be empty when the HTTP method is “GET” or “POST - multipart/form-data”.

获取查询字符串()

Return the query string of the page was accessed, if any.

获取参数(参数名称)

args parsed from ^{tt23}$ when the request is sent by “GET” or “POST - multipart/form-data”.

args parsed from ^{tt24}$ when the request is sent by “POST - application/x-www-form-urlencoded”.

获取标题(标题名称)

Return the header`s value of the ^{tt25}$, if any.

X100级响应

设置正文(内容)

Set the response data to visitor.

Type ‘str’ and type ‘bytes’ are both accepted.

设置头(名称、值)

Set the HTTP header.

http错误500

visitor will get HTTP error “500” when the handler function of the url he visit raise an error or code something wrong.

支持的Python版本

x100http only supports python 3.4 or newer, because of ^{tt26}$ and ^{tt27}$.

示例

获取访客IP

from 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)

方法后途径

from 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)

处理文件上传

from 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)

设置http头

from 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)

更灵活的路由< EH3>
from 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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JMS 1.1:PointtoPoint模式的负载共享客户端   java如何从多个git存储库运行gatling   java拆分二维数组并将其转换为一维数组   Java选择音频设备并播放。mp3   java如何用GSON解析JSON文件   mysql Hibernate无法设置java。整型字段。XXX。XXX。XXX。坐标java的id。util。ArrayList   URL代码帮助请输入短代码(新手Java程序员)   java为什么removeall()方法没有从JFrame中删除所有组件?   java如何在HbaseMapReduce作业中提供带有表的名称空间   java Android Libgx青蛙游戏崩溃:找不到类的com。伦格伯特。青蛙。青蛙游戏'   编写自定义事件时的java侦听器接口   通过单击按钮,java每天都有新的活动   java Jetty IllegalStateException:无会话管理器   我正在尝试使用java将二进制文件转换为ascii和XML,我需要一个ASN包。1语法   java在可调用文件中等待回调   java io。内蒂。util。Netty中的非法引用计数异常:refCnt:0   用于Hibernate表和列的java自动保留字转义   将文件数组放入java类   使用开源作业调度器运行php文件的java