由于HTTP post中的日期和时间,Python代码失败

由于HTTP post中的日期和时间,Python代码失败,python,json,post,Python,Json,Post,我试图通过评论向Virustotal报告恶意IP 此代码适用于: import requests #import datetime #import time ip = open('/home/pi/upload.log', 'r') #today = datetime.datetime.now() for x in ip: try: ip = x.rstrip() url = "https://www.virustotal.com/api/v3/ip_addres

我试图通过评论向Virustotal报告恶意IP

此代码适用于:

import requests
#import datetime
#import time

ip = open('/home/pi/upload.log', 'r')
#today = datetime.datetime.now()
for x in ip:
  try:
    ip = x.rstrip()
    url = "https://www.virustotal.com/api/v3/ip_addresses/" + ip + "/comments"
    print(url)
    print(ip)
    payload = '{"data":{"type":"comment","attributes":{"text":"This IP was carrying out SSH bruteforce attack"}}}'
    headers = {
        'Content-Type': 'application/json',
        'x-apikey': 'api key here'
}
    response = requests.request("POST", url, headers=headers, data = payload)
    print(response)
    print(response.text.encode('utf8'))
    time.sleep(16)
  except:
    pass
这是不起作用的代码:

import requests
import datetime
import time

ip = open('/home/pi/upload.log', 'r')
today = datetime.datetime.now()
for x in ip:
  try:
    ip = x.rstrip()
    url = "https://www.virustotal.com/api/v3/ip_addresses/" + ip + "/comments"
    print(url)
    print(ip)
    payload = {"data":{"type":"comment","attributes":{"text":'This IP was carrying out SSH bruteforce attack on '  + today.strftime("%d-%b-%Y (%H:%M:%S.%f)")}}}
    headers = {
        'Content-Type': 'application/json',
        'x-apikey': 'api key here'
}
    response = requests.request("POST", url, headers=headers, data = payload)
    print(response)
    print(response.text.encode('utf8'))
    time.sleep(16)
  except:
    pass
我得到的错误是:

<Response [400]>
{
    "error": {
        "code": "BadRequestError",
        "message": "Malformed JSON"
    }
}
https://www.virustotal.com/api/v3/ip_addresses/5.182.39.61/comments

{
“错误”:{
“代码”:“BadRequestError”,
“消息”:“格式错误的JSON”
}
}
https://www.virustotal.com/api/v3/ip_addresses/5.182.39.61/comments
我不确定我错在哪里。将日期作为威胁情报的一部分,有助于其他正在查找IP声誉的人


要使代码正常工作,我需要做哪些更改?

在第二段代码中,您没有将负载转换为JSON字符串:

import json
...
response = requests.request("POST", url, headers=headers, data = json.dumps(payload))
...