Python http.client 库,发请求,返回 200,但是 response 是空的

wendy1 · 2019年01月31日 · 最后由 arrow 回复于 2019年02月02日 · 3833 次阅读

发请求,返回 200,但是 response 是空的,这是为什么

import http.client
import urllib.parse
params = urllib.parse.urlencode({"alarmId":"10-40-226.108-5117-4f5b-97b9-000000AAA1",
"alarmTypeCode":"403001","alarmTypeDesc":"描述 1",
"alarmStartTime":1548814332000,"alarmEndTime":1548817200000,
"alarmStatus":0,"relatedCameraIndexCode":"001130","relatedCameraName":"IPDOME-67.100"})
headers = {'Content-Type':'application/json','appKey':'appCF4QCVP','appSecret':1548931565000,
'wjwAuthorization':'2dbc04dea23510f83390f5a749f5e9587d8984b397b3b4b695f3d835069f3544',
'cache-control':'no-cache','User-Agent':'Mozilla/5.0(Windows NT6.1;WOW64;Trident/7.0;rv:11.0) like Gecko',
'Accept-Encoding':'gzip,deflate','Connection': 'Keep-Alive','Accept': '/'}
conn = http.client.HTTPConnection("10.40.226.108", "80", timeout=5)
conn.request("POST", "http://10.40.226.108/eop/services/common/post/audioAlarmReceive", params, headers)

response = conn.getresponse()
data = response.read()
print(data)
conn.close()

最佳回复

找了个接口试了下,是可以拿到数据的,这个要查下是不是后端接口本来就没返回数据。
python http 推荐用 requests

import json
import requests


headers = {
    'Content-Type': 'application/json',
    'appKey': 'appCF4QCVP',
    'appSecret': '1548931565000',
    'wjwAuthorization': '2dbc04dea23510f83390f5a749f5e9587d8984b397b3b4b695f3d835069f3544',
    'cache-control': 'no-cache',
    'User-Agent': 'Mozilla/5.0(Windows NT6.1;WOW64;Trident/7.0;rv:11.0)like Gecko',
    'Accept-Encoding': 'gzip,deflate',
    'Connection': 'Keep-Alive',
    'Accept': '/'
}
data = {
    "alarmId": "10-40-226.108-5117-4f5b-97b9-000000AAA1",
    "alarmTypeCode": "403001",
    "alarmTypeDesc": "描述1",
    "alarmStartTime": 1548814332000,
    "alarmEndTime": 1548817200000,
    "alarmStatus": 0,
    "relatedCameraIndexCode": "001130",
    "relatedCameraName": "IPDOME-67.100"
}
resp = requests.post(
    url='http://10.40.226.108/eop/services/common/post/audioAlarmReceive', 
    data=json.dumps(data, ensure_ascii=False).encode(), 
    headers=headers
)
print(resp.text)
共收到 1 条回复 时间 点赞

找了个接口试了下,是可以拿到数据的,这个要查下是不是后端接口本来就没返回数据。
python http 推荐用 requests

import json
import requests


headers = {
    'Content-Type': 'application/json',
    'appKey': 'appCF4QCVP',
    'appSecret': '1548931565000',
    'wjwAuthorization': '2dbc04dea23510f83390f5a749f5e9587d8984b397b3b4b695f3d835069f3544',
    'cache-control': 'no-cache',
    'User-Agent': 'Mozilla/5.0(Windows NT6.1;WOW64;Trident/7.0;rv:11.0)like Gecko',
    'Accept-Encoding': 'gzip,deflate',
    'Connection': 'Keep-Alive',
    'Accept': '/'
}
data = {
    "alarmId": "10-40-226.108-5117-4f5b-97b9-000000AAA1",
    "alarmTypeCode": "403001",
    "alarmTypeDesc": "描述1",
    "alarmStartTime": 1548814332000,
    "alarmEndTime": 1548817200000,
    "alarmStatus": 0,
    "relatedCameraIndexCode": "001130",
    "relatedCameraName": "IPDOME-67.100"
}
resp = requests.post(
    url='http://10.40.226.108/eop/services/common/post/audioAlarmReceive', 
    data=json.dumps(data, ensure_ascii=False).encode(), 
    headers=headers
)
print(resp.text)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册