服务器在发布请求时未提交表单 - Python

问题描述:

我在发布请求时遇到了来自Web服务器的响应问题。网络服务器是NanoDLP,我正在尝试编写一个脚本,用于在提交表单时加载3D打印文件。花了几个小时阅读论坛和“帖子”,涵盖了这个话题,我看不出做错了什么。有人可以看一看,看看他们能帮助我吗?代码如下:服务器在发布请求时未提交表单 - Python

import requests 

machineAddr = "http://192.168.0.234" 

# Get printable files from USB 
urlUSBFiles = machineAddr + "/json/usb" 
usbFiles = requests.get(urlUSBFiles).json() 
print(usbFiles) 

fileUploadName = input('What do you want to name your file?') 
fileUploadData = { 
     'USBfile': usbFiles[1], 
     'Path': fileUploadName, 
     'ProfileID': '3', 
     'AutoCenter': '0', 
     'StopLayers': '', 
     'LowQualityLayerNumber': '0', 
     'MaskFile': '', 
     'MaskEffect': '', 
     'ImageRotate': '0' 
} 

print(fileUploadData) 

urlAddUSBFiles = machineAddr + "/plate/add-usb" 
r = requests.post(urlAddUSBFiles, data=fileUploadData) 
print(r) 

下面是当代码运行响应:

['/media/usb0/DriveSleeve.stl', '/media/usb0/TestCube100um.zip'] 

What do you want to name your file?turbo {'USBfile': '/media/usb0/TestCube100um.zip', 'Path': 'turbo', 'ProfileID': '3', 'AutoCenter': '0', 'StopLayers': '', 'LowQualityLayerNumber': '0', 'MaskFile': '', 'MaskEffect': '', 'ImageRotate': '0'} <Response [200]> Process finished with exit code 0

感谢,

迪伦

+0

要调用POST Web服务,您需要使用requests.post方法而不是get。 – notionquest

+0

@notionquest是的,这是在代码的底部完成的。第一部分读取USB驱动器的内容 - 完美工作。第二个最后一行是给我的问题。 – Dylan144GT

+0

什么是错误信息?你能显示错误信息吗?你得到了什么http状态码(即400,500等)? – notionquest

为了完整起见,我发现我的问题。我没有定义上传数据作为一个字典,所以解决方法是:

fileUploadData = dict(
    USBfile = usbFiles[1], 
    Path = fileUploadName, 
    ProfileID = 3, 
    AutoCenter = 0, 
    StopLayers = '', 
    LowQualityLayerNumber: 0, 
    MaskFile = '', 
    MaskEffect = '', 
    ImageRotate = 0 
) 

我希望这可以帮助其他人有类似的问题! :)