Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';无法在Twisted中上载文件_Python_Upload_Twisted_Twisted.web - Fatal编程技术网

Python Can';无法在Twisted中上载文件

Python Can';无法在Twisted中上载文件,python,upload,twisted,twisted.web,Python,Upload,Twisted,Twisted.web,我正在尝试运行一个简单的服务,该服务将通过HTTPPOST接受文件上传,但是在POST请求完成后,我很难确定实际文件数据驻留在哪里 我得到的代码是: from twisted.web.server import Site from twisted.web.resource import Resource from twisted.internet import reactor #import cgi class FormPage(Resource): def render_GET(s

我正在尝试运行一个简单的服务,该服务将通过HTTPPOST接受文件上传,但是在POST请求完成后,我很难确定实际文件数据驻留在哪里

我得到的代码是:

from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor

#import cgi

class FormPage(Resource):
    def render_GET(self, request):
        return """
<html>
<body>
<form method="POST">
    Text: <input name="text1" type="text" /><br />
    File: <input name="file1" type="file" /><br />
    <input type="submit" />
</form>
</body>
</html>
"""

    def render_POST(self, request):
        response = '<p>File: %s</p>' % request.args['file1'][0]
        response += '<p>Content: %s</p>' % request.content.read()
        return '<html><body>You submitted: %s</body></html>' % (response)

root = Resource()
root.putChild("form", FormPage())
factory = Site(root)
reactor.listenTCP(8080, factory)
reactor.run()
从twisted.web.server导入站点
从twisted.web.resource导入资源
从twisted.internet导入
#导入cgi
类FormPage(资源):
def render_GET(自我,请求):
返回“”
文本:
文件:
""" def render_POST(自我,请求): 响应='文件:%s

'%request.args['file1'][0] 响应+='内容:%s

'%request.Content.read() 返回“您提交了:%s%”(响应) 根=资源() root.putChild(“form”,FormPage()) 工厂=站点(根) 反应堆listenTCP(8080,工厂) 反应堆运行()
我得到的答复是:

您提交了:

文件:test.txt

内容:text1=sdfsd&file1=test.txt

我能找到的关于该主题的大部分内容都是使用
IRequest
对象,该对象假定具有
IRequest.files
属性,但它没有列在中,并且在我运行它时会抛出一个错误


收到文件后的最终目标是将其移动到特定文件夹,然后运行一些进一步的代码。

我也有类似的问题。事实上,你在你的问题下的评论中解决了这个问题,但为了澄清和总结,我写下了这个答案

为了上传文件,您需要将表单的enctype设置为多部分/表单数据,如下所示:

<form enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" name="submit" value="Submit">
</form>

我不确定这些参数(名称和文件名)是否总是按照这个特定的顺序排列,所以要小心。还要记住,如果您有多个文件输入,它将不起作用。

我也有类似的问题。事实上,你在你的问题下的评论中解决了这个问题,但为了澄清和总结,我写下了这个答案

为了上传文件,您需要将表单的enctype设置为多部分/表单数据,如下所示:

<form enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" name="submit" value="Submit">
</form>

我不确定这些参数(名称和文件名)是否总是按照这个特定的顺序排列,所以要小心。还请记住,如果您有多个文件输入,它将不起作用。

请注意,因为我现在没有时间给出完整的答案:您链接到的其他问题是关于
twisted.web2
,这是一个现在删除的对
twisted.web
的部分重写。从我们最早的一张罚单中可以看出,文件上传确实有点像是吸进去的扭曲:而且文档也有严重的问题:很抱歉,您遇到了麻烦@Glyph结果是我的表单HTML中缺少了多部分
enctype
。我有数据,但现在我发现文件名不再在post数据中。它永远消失了吗?我想是的。很抱歉Web浏览器也不太好:)@Glyph它不是浏览器,我可以在原始帖子数据中看到文件名。twisted.web对此没有任何作用。我现在没有时间给出完整的答案,所以只需简单说明一下:您链接到的其他问题是关于twisted.web2的,现在已删除对twisted.web的部分重写。从我们最早的一张罚单中可以看出,文件上传确实有点像是吸进去的扭曲:而且文档也有严重的问题:很抱歉,您遇到了麻烦@Glyph结果是我的表单HTML中缺少了多部分
enctype
。我有数据,但现在我发现文件名不再在post数据中。它永远消失了吗?我想是的。很抱歉Web浏览器也不太好:)@Glyph它不是浏览器,我可以在原始帖子数据中看到文件名。twisted.web对它没有任何作用。实际上,我已经用一些其他修复程序对其进行了修改,调用
re.search(r'name=“file”filename=“(.*)”,content)有点费劲(r'name=“file”;filename=“(.*)”,content)
解析器:(我实际上用一些其他修复程序对其进行了修改,调用
re.search(r'name=“file”;filename=“(.*)”,content)有点费劲)
a解析器:(