Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- api_key='x'
- secret_key='x'
- httpClient=requests.Session()
- recv_window=str(5000)
- def genSignature(timestamp, payload):
- param_str= f"{timestamp}{api_key}{recv_window}".encode() + payload
- hash = hmac.new(bytes(secret_key, "utf-8"), param_str, hashlib.sha256)
- signature = hash.hexdigest()
- return signature
- image_path = r"D:\1111.png"
- url = "https://api.bybit.com/v5/p2p/oss/upload_file"
- # params
- boundary = "boundary-file"
- filename = "test.png"
- mimeType = "image/png"
- with open(image_path, "rb") as f:
- binary_data = f.read()
- files = MultipartEncoder(
- {
- 'upload_file': (
- filename,
- open(image_path, "rb"),
- mimeType
- )
- },
- boundary=boundary
- )
- payload = (
- f"--{boundary}\r\n"
- f"Content-Disposition: form-data; name=\"upload_file\"; filename=\"{filename}\"\r\nContent-Type: {mimeType}\r\n\r\n"
- ).encode() + binary_data + f"\r\n--{boundary}--\r\n".encode()
- timestamp = str(int(time.time() * 10 ** 3))
- signature = genSignature(timestamp, payload)
- headers = {
- 'X-BAPI-API-KEY': api_key,
- 'X-BAPI-SIGN': signature,
- 'X-BAPI-SIGN-TYPE': '2',
- 'X-BAPI-TIMESTAMP': timestamp,
- 'X-BAPI-RECV-WINDOW': recv_window,
- "Content-Type": "multipart/form-data; boundary=" + boundary
- }
- response = httpClient.request("POST", url, headers=headers, data=files.to_string())
- print(response.status_code)
- print(response.text)
Advertisement