【问题标题】:how to convert curl command to python3 requests如何将 curl 命令转换为 python3 请求
【发布时间】:2017-12-27 15:03:29
【问题描述】:

谁能告诉我如何将下面的 CURL 命令转换为 Python3 请求模块?

卷曲命令 :

curl --header "Accept=application/json" --form 
               print_file=@a.gcode http://189.167.1.103/print_file_uploads
  1. 其实我要上传一个Gcode文件到3D打印机。

  2. 我成功使用了模块 OS。我就是这样做的。

成功 :

import os
ip_address = 189.167.1.103
file_name = "D:\networking\a.gcode"
os.system("curl --header "Accept=application/json" --form 
print_file=@%shttp://%s/print_file_uploads"%(file_name,ip_address))

但我不希望它与操作系统模块一起完成。你能帮我在 python3 请求中实现同样的功能吗?

【问题讨论】:

  • 请详细描述初始 curl 命令的作用,我相信有人会很乐意帮助您

标签: python-3.x python-2.7 curl python-requests


【解决方案1】:

首先你可以阅读this

所以,请求的例子是:

import requests
response = requests.get('https://github.com/timeline.json')
response.json()

您可能的解决方案是:

import requests

url = 'http://189.167.1.103/print_file_uploads'
payload = open("D:\networking\a.gcode")
headers = {'Accept: application/json'}
response = requests.post(url, data=payload, headers=headers)

【讨论】:

    【解决方案2】:

    这可能有效-

    导入请求

    access_token = 'xxxx' device_id = 'xxxx'

    地址 = 'https://api.spark.io/v1/devices/{0}/update/'.format(device_id) 数据 = {'access_token': access_token, 'args': '000000000000000'}

    r = requests.post(地址,数据=数据)

    打印(r.text)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 2016-08-04
      • 2018-10-21
      • 2017-05-25
      • 2014-04-12
      • 2018-05-14
      • 2021-04-28
      • 2021-12-05
      相关资源
      最近更新 更多