zoukankan      html  css  js  c++  java
  • python之web开发(待续)

    WSGI接口

    无论多么复杂的Web应用程序,入口都是一个WSGI处理函数。

    HTTP请求的所有输入信息都可以通过environ获得,HTTP响应的输出都可以通过start_response()加上函数返回值作为Body。

    其实一个Web App,就是写一个WSGI的处理函数,针对每个HTTP请求进行响应。

    #hello.py
    
    def application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return '<h1>Hello, web!<h1>'
    #server.py
    
    from wsgiref.simple_server import make_server
    from hello import application
    
    httpd = make_server('', 8000, application)
    print 'Serving Http on port 8000'
    httpd.serve_forever()

    python cgi模块

    CGI script is invoked by an HTTP server, usually to process user input submitted through an HTML <FORM> or <ISINDEX> element.

    往往用 FieldStorage 去处理表单数据,且FieldStorage的实例就像字典一样处理。

    但如果表单中提交的数据为空, 那么那个数据将不会在FieldStorage中显示, 但可以通过设置keep_blank_values=True使其显示

    ps:

    If the submitted form data contains more than one field with the same name, the object retrieved by form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances.

    If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute.

    #form数据在self._environ['wsgi.input']中, request的数据在self._environ中
    form = cgi.FieldStorage(fp=self._environ['wsgi.input'], environ=self._environ, keep_blank_values=True)
    normal
    = form[key].value
    if isinstance(form[key], list): fileitem = form[key] if fileitem.filename: # It's an uploaded file; count lines linecount = 0 while 1: line = fileitem.file.readline() if not line: break linecount = linecount + 1
  • 相关阅读:
    <排序算法> 计数排序CountSort
    <排序算法> 直接插入排序InsertSort
    <排序算法> 快速排序QuickSort
    C# 获取文件的后缀,文件名和路径
    C# 移除HTML标签
    无限层级数
    iOS通用链接配置
    iOS 最新证书制作流程、制作通用证书
    C# FormData 文件太大报错404 Form表单上传大文件,无法进入后台Action,页面提示404.
    IS加载JSON 和 MP4文件 错误 404 提示 需要添加mime映射(默认IIS Express里没有映射)
  • 原文地址:https://www.cnblogs.com/whuyt/p/4496770.html
Copyright © 2011-2022 走看看