我正在努力使用python requests提交特定表单。我想使用它的网站上的其他表单工作正常,我可以提交登录表单等。这只是我遇到问题的文件上传。显然,提交表单效果很好,因为我从网站收到一条消息,说“请返回并选择至少一个要上传的图像文件”。但该文件未发布到该网站。
如果我使用 Firefox 插件 Tamperdata 查看请求,在提交空表单后,它看起来像这样:
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="userfile[]"; filename=""\r
Content-Type: application/octet-stream\r
\r
\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="userfile[]"; filename=""\r
Content-Type: application/octet-stream\r
\r
\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="userfile[]"; filename=""\r
Content-Type: application/octet-stream\r
\r
\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="userfile[]"; filename=""\r
Content-Type: application/octet-stream\r
\r
\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="userfile[]"; filename=""\r
Content-Type: application/octet-stream\r
\r
\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="upload_to"\r
\r
0\r
-----------------------------201075691119887339851216603987\r
Content-Disposition: form-data; name="upload_type"\r
\r
standard\r
-----------------------------201075691119887339851216603987--\r
Run Code Online (Sandbox Code Playgroud)
(我用换行符替换了所有 \n 以使其更具可读性)
我的 python 上传代码如下所示:
index = s.get("https://example.com/")
file = {'userfile[]': open('tmp/cover/cover.jpg', 'rb')} …Run Code Online (Sandbox Code Playgroud) 我使用nginx在同一个域下提供静态html站点和expressjs应用程序.我的nginx配置看起来像这样:
location / {
try_files $uri $uri/ /index.html;
autoindex off;
root /var/www/example.com/static/;
}
location /admin {
proxy_pass http://localhost:3007/;
proxy_set_header Host $host;
proxy_buffering off;
autoindex off;
}
Run Code Online (Sandbox Code Playgroud)
如果我访问example.com/admin,我可以访问在端口3007上运行的应用程序,所以看起来我app.get('/', routes.index);正在工作,但是我遇到了一些麻烦让我的其他路由工作.
例如:
app.get('/admin/test', function(req, res) {
console.log("test!")
});
Run Code Online (Sandbox Code Playgroud)
如果我尝试访问,example.com/admin/test我会Cannot GET //test根据我的日志看到和404:
GET //test 404 11ms
Run Code Online (Sandbox Code Playgroud)
如果我改变路线/测试它是同样的问题.任何指针都有什么不对.我还没有找到一种方法来相对于它们所安装的基本路径(/ admin)制作路由.
谢谢!