How does Python execute the curl-T command?

curl -v "https://developer.api.autodesk.com/oss/v2/buckets/mybucket/objects/example.txt"
  -X "PUT"
  -H "Authorization: Bearer AlmsGlZlqz1JkkzEruUdINMKA6IF"
  -H "Content-Type: text/plain; charset=UTF-8"
  -T "example.txt"

this is a curl file transfer command provided on the froge platform. How to solve it with Python https://forge.autodesk.com/en.:bucketKey-objects-:objectName-PUT/-sharpbody-structure.

I don"t know how to transfer the code I implemented in urllib2.

f = codecs.open(file_name, "rb")
        filebody = f.read()
        PostUrl = " https://developer.api.autodesk.com/oss/v2/buckets/"+bucketKey+"/objects/"+fileinfo.name
        bodys = {}
        bodys[""] = filebody
        
        postData = bodys
        -sharp postData = {"file": filebody}
        headers = {"Authorization":"Bearer "+token,"Content-Length":fileinfo._size}
        request = urllib2.Request(PostUrl,postData)
        
        request.add_header("Authorization","Bearer "+token)
        request.add_header("Content-Length",fileinfo._size)
        request.get_method = lambda:"PUT"

        ctx = ssl.create_default_context()
        ctx.check_hostname = False
        ctx.verify_mode = ssl.CERT_NONE
        response = urllib2.urlopen(request,context=ctx)
        result = response.read()
        result = eval(result)
        print "------", result
        response_data["results"] = result 

error report:

Traceback (most recent call last):
  File "D:\ZZKJPRO\WuhanPro\src\modalManager\views.py", line 707, in autodesk_up
loadfile
    response = urllib2.urlopen(request,context=ctx)
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 429, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 447, in _open
    "_open", req)
  File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1241, in https_open
    context=self._context)
  File "C:\Python27\lib\urllib2.py", line 1195, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\lib\httplib.py", line 1042, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 1082, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 1038, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 886, in _send_output
    self.send(message_body)
  File "C:\Python27\lib\httplib.py", line 858, in send
    self.sock.sendall(data)
  File "C:\Python27\lib\ssl.py", line 753, in sendall
    v = self.send(data[count:])
TypeError: unhashable type
May.11,2021

postData has a problem, just make postData=f better


you can use the requests class library instead, as follows

-sharp -*- coding: utf-8 -*-
'''
curl -v 'https://developer.api.autodesk.com/oss/v2/buckets/mybucket/objects/example.txt'
  -X 'PUT'
  -H 'Authorization: Bearer AlmsGlZlqz1JkkzEruUdINMKA6IF'
  -H 'Content-Type: text/plain; charset=UTF-8'
  -T 'example.txt'
'''
import requests


filename = 'example.txt'
with open(filename, 'rb') as f:
    data = f.read()


url = f'https://developer.api.autodesk.com/oss/v2/buckets/mybucket/objects/{filename}'
headers = {
    'Authorization': 'Bearer AlmsGlZlqz1JkkzEruUdINMKA6IF',
    'Content-Type': 'text/plain; charset=UTF-8',
}
resp = requests.put(url, data=data, headers=headers)
print('HTTP {}\n{}\n\n{}'.format(resp.status_code, resp.headers, resp.text))
Menu