简体   繁体   English

尝试使用python请求模块进行发布请求时出现HTTP 500错误

[英]http 500 error when attempting to make a post request with python requests module

I have a python (python 2.7) script that uses the requests module to make a post to a web application I have running on the localhost. 我有一个python(python 2.7)脚本,它使用请求模块向我在本地主机上运行的Web应用程序发布信息。 The form that needs to be filled out has areas for data and areas for file uploads. 需要填写的表单具有用于数据的区域和用于文件上传的区域。

import requests
root = "http://localhost/qatrack/"
test_list_url =root+"qa/utc/perform/17/day=next&next=/qatrack/qa/unit/7/"
s = requests.Session()
s.get(login_url)
token = s.cookies['csrftoken']
login_data = {
    'username':'user',
    'password':'pass',
    'csrfmiddlewaretoken': token
}
login_resp = s.post(login_url, data=login_data)
data1=open('C:/deploy/qatrackplus/python/imgs/test1.png','rb')
data2=open('C:/deploy/qatrackplus/python/imgs/test2.png','rb')
test_data = {
    'csrfmiddlewaretoken': token,
    "work_started":timestr,
    "work_completed":timestr,
    "status":"1",
    "form-TOTAL_FORMS":"4",
    "form-INITIAL_FORMS":"4",
    "form-MAX_NUM_FORMS":"1000",
    "form-0-value":"5"
}
f={
    "form-1-string_value":data1,
    "form-2-string_value":data2
}
resp = s.post(test_list_url, data=test_data, files=f)

The response gives a 500 error code, as well when I put the input to an .html file, it will say that there is an Attribute error in one of the scripts of the web application. 响应给出了500个错误代码,当我将输入内容放入.html文件时,它将说Web应用程序的一个脚本中存在属性错误。 I do not get this if I run the script for a form that does not have a file upload that needs to be filled. 如果我为没有需要上载的文件上载的表单运行脚本,则不会得到此消息。

I think your problem comes from how you are passing data1 and data2 to the form. 我认为您的问题出在您如何将data1data2传递给表单。 You are calling open() on the image files, but that doesn't give you any data by itself. 您正在对图像文件调用open() ,但这本身并不能提供任何数据。 It gives you a file reader object. 它为您提供了一个文件读取器对象。 To get the data from it, you need to use something like read() to actually get the data from the stream so you can pass it on. 要从中获取数据,您需要使用read()类的东西来从流中实际获取数据,以便继续传递。

As an update to this, I've figured out the solution. 作为对此的更新,我已经找到了解决方案。 It was looking for a filename, however based on the uploading mechanism of the web application it was looking for it in a folder C:/deploy/.../media/uploads/tmp/. 它正在寻找文件名,但是基于Web应用程序的上传机制,它正在文件夹C:/ deploy /.../ media / uploads / tmp /中寻找文件名。 Upon saving the files to be uploaded there, removing the files=f parameter in the post request and submitting the test_data form as: 保存要上传到那里的文件后,在发布请求中删除files = f参数,并提交test_data表单为:

test_data = {
  'csrfmiddlewaretoken': token,
  "work_started":timestr,
  "work_completed":timestr,
  "status":"1",
  "form-TOTAL_FORMS":"3",
  "form-INITIAL_FORMS":"3",
  "form-MAX_NUM_FORMS":"1000",
  "form-0-value":"5"
  "form-1-string_value":test1.png #saved in C:/deploy/.../media/uploads/tmp/
  "form-2-string_value":test2.png
}
 resp = s.post(test_list_url, data=test_data)

The files uploaded as desired. 根据需要上传文件。 Thank you for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带/ post的HTTP Post URL和不带Python请求模块的HTTP Post URL有什么区别? - What is the difference between HTTP Post URL with /post and without using Python requests module? 如何发出异步HTTP POST请求 - How to make asynchronous HTTP POST requests 尝试POST时出现“ getaddr信息失败”错误 - “getaddr info failed” Error When Attempting POST 当 URL 不存在时 Python 请求模块中的错误处理 - Error Handling in Python Requests Module When URL Does Not Exist python请求http响应500(可以在浏览器中访问站点) - python requests http response 500 (site can be reached in browser) 非常慢 http 在 python 中发布请求 - Very slow http post requests in python 发送HTTP Post Requests在Fiddler中工作,但在Python中不起作用 - Sending HTTP Post Requests work in Fiddler but not in Python request.post返回HTTP 200 python - requests.post return HTTP 200 python Python 2.7和Flask-使用venv时,使用“ Random”模块中的函数返回“ 500 Internal Server Error” - Python 2.7 and Flask - using functions from “Random” module returns “500 Internal Server Error” when using venv 使用Livy连接到Kerberized Hadoop集群时,Python请求Post请求失败 - Python Requests Post request fails when connecting to a Kerberized Hadoop cluster with Livy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM