寻找wsgi处理表单的方式,比如mod_python Publisher

寻找wsgi处理表单的方式,比如mod_python Publisher,python,mod-wsgi,wsgi,mod-python,Python,Mod Wsgi,Wsgi,Mod Python,在mod_python Publisher中,我可以使用以下代码编写文件加载器(本例中为uploader.py): def index(): html = ''' <html> <body> <form id="fileUpload" action="uploader.py/upload" enctype="multipart/form-data" method="post"> <input

在mod_python Publisher中,我可以使用以下代码编写文件加载器(本例中为uploader.py):

    def index():
        html = '''
    <html>
    <body>
    <form id="fileUpload" action="uploader.py/upload" enctype="multipart/form-data" method="post">
        <input type="file" id="file" name="file_name"/>
        <input type="submit" value="Upload"/>
    </form>
    </body>
    </html>
    '''
        return html

    def upload(req, file_name):
        fd = open("/mydir/myfile", "w")
        for line in file_name.file.readlines():
            fd.write(line)
        fd.close()
def index():
html=“”
'''
返回html
def上传(请求、文件名):
fd=open(“/mydir/myfile”,“w”)
对于文件_name.file.readlines()中的行:
写入(行)
fd.close()
这个例子排除了任何错误检查,但说明了我可以用mod_python和Publisher做什么。关键是Publisher能够在文件uploader.py中调用my函数upload,并将参数file_name与表单中选择的文件的属性file一起传递给它

我想做的是在mod_wsgi中做同样的事情。我正在寻找一种从表单中调用类似函数的方法,并以类似的方式调用我的函数。我已经看过webob,但不知道如何用这个包做同样的事情。如果您能在不必使用mod_python和Publisher的情况下编写类似的解决方案,我将不胜感激。

答案从问题文本迁移而来

经过一番挖掘,我想出了解决这个问题的办法。这就是我想出的解决办法。我用两个文件实现了这一点:uploader1.py和uploader2.py。这是uploader1.py的代码:

    def application(environ, start_response):
        from webob import Response
        html = '''
        <html>
        <body>
        <h1>Upload</h1>
        <form id="fileUpload" action="uploader2.py" enctype="multipart/form-data" method="post">
        <input type="file" name="filename"/>
        <input type="submit" value="Upload"/>
        </form>
        </body>
        </html>
        '''
        response = Response(body = html,
        content_type = "text/html",
        charset = "utf8",
        status = "200 OK")
        return response(environ, start_response)
def应用程序(环境,启动响应):
来自webob导入响应
html=“”
上传
'''
response=response(body=html,
content\u type=“text/html”,
charset=“utf8”,
状态=“200正常”)
返回响应(环境、启动和响应)
这是uploader2.py的代码:

    import os
    import cgi

    def application(environ, start_response):
        from webob import Response

        form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=True)
        try:
            fileitem = form['filename']
        except KeyError:
            fileitem = None

        if fileitem is not None and fileitem.filename and fileitem.file:
            # remove any relative paths to prevent directory traversal attacks
            fn = os.path.basename(fileitem.filename)
            # directory to upload to
            fn = "/tmp/"+fn
            with open(fn, 'w') as f:
                # this upload is for text files
                lines = fileitem.file.readlines()
                for line in lines:
                    f.write(line)

            f.close()
            message = "Uploaded file: %s" % fn
        else:
            message = "Error: no file selected for upload"
        html = '''
        <html>
        <body>
        <p>%s
        </body>
        </html>
        ''' % message
        response = Response(body = html,
        content_type = "text/html",
        charset = "utf8",
        status = "200 OK")
        return response(environ, start_response)
导入操作系统
导入cgi
def应用程序(环境、启动和响应):
来自webob导入响应
form=cgi.FieldStorage(fp=environ['wsgi.input'],environ=environ,keep_blank_values=True)
尝试:
fileitem=form['filename']
除KeyError外:
fileitem=None
如果fileitem不是None,则fileitem.filename和fileitem.file:
#删除任何相对路径以防止目录遍历攻击
fn=os.path.basename(fileitem.filename)
#要上载到的目录
fn=“/tmp/”+fn
以开放式(fn,'w')作为f:
#此上载用于文本文件
lines=fileitem.file.readlines()
对于行中的行:
f、 写(行)
f、 关闭()
message=“上载的文件:%s”%fn
其他:
message=“错误:未选择要上载的文件”
html=“”
%
''消息
response=response(body=html,
content\u type=“text/html”,
charset=“utf8”,
状态=“200正常”)
返回响应(环境、启动和响应)

与mod_python实现相比,此实现的唯一缺点是我无法找到在单个文件中实现此解决方案的方法。mod_python允许我调用模块内的函数,但我无法找到使用mod_wsgi实现这一点的方法。另外,在调试过程中,如果能够将输出记录到我为mod_wsgi设置的同一个自定义错误日志文件中会很好,但我找不到这样做的方法,因此必须使用print语句记录到主Apache日志文件。如果有人有办法做到这一点,我很乐意知道一个解决方案。

看看这个流行的框架概述:。