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

时间:2017-10-12 15:07:11

标签: python post python-requests

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

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

谢谢,

迪伦

1 个答案:

答案 0 :(得分:0)

为了完整起见,我发现了我的问题。我没有将上传数据定义为dict,因此修复是:

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

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