python 小白 : 关于 curl 转换成 requests 求助

原厂给的代码 :

 #!/bin/bash
username="example_username"
apiKey="example_apiKey"
date=`env LANG="en_US.UTF-8" date -u "+%a, %d %b %Y %H:%M:%S GMT"`
password=`echo -en "$date" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64`
curl -i --url "https://open.chinanetcenter.com/api/report/domainflow?datefrom=2017-11-07T00:00:00%2B08:00&dateto=2017-11-07T00:15:00%2B08:00&type=fiveminutes" \
-X "POST" \
-u "$username:$password" \
-H "Date: $date" \
-H "Accept: application/json" \
-d '<?xml version="1.0" encoding="utf-8"?>
<domain-list>
  <domain-name>www.example1.com</domain-name>
</domain-list>
'

试着改成 python 一直改不出来 一直出现

<Response [400]>

我改成 python 应该有错吧 .

import requests
import datetime
import hmac
import hashlib
import base64
username = "example_username"
apiKey= 'example_apiKey'
now = datetime.datetime.now()
nowTime = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
nowTime_bytes = bytes(nowTime, encoding='utf-8')
value = hmac.new(apiKey, nowTime_bytes, hashlib.sha1).digest()
token = base64.b64encode(value).rstrip()
headers = {"Accept": "application/json",
           "Date": nowTime}
url = 'https://open.chinanetcenter.com/api/report/domainflow'
payload= {'datefrom':'2017-11-07T00:00:00%2B08:00',
          'dateto':'2017-11-07T00:15:00%2B08:00',
          'type': 'fiveminutes'}
xml = '<?xml version="1.0" encoding="utf-8"?>
 <domain-list>
   <domain-name>www.example1.com</domain-name>
 </domain-list>
 '

r = requests.post(url, headers=headers, auth=(username, token), data=xml,params=payload)
print (r)

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问