簡體   English   中英

如何將 curl 命令轉換為 python3 請求

[英]how to convert curl command to python3 requests

誰能告訴我如何將下面的這個 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))

但我不想用 OS 模塊來完成。 你能幫我在 python3 請求中實現相同的嗎?

首先你可以閱讀這個

因此,請求的示例是:

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)

這可能有效-

進口請求

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM