为了账号安全,请及时绑定邮箱和手机立即绑定

同样的参数使用requests库得不到返回结果,而httplib可以正确返回。可能的原因是什么?


# -*- coding: utf-8 -*-

import requests
import json

url = 'http://www.demodemo.cc/rpc/IUserService'
params = {
   "jsonrpc": "1.1",
   "method": "getAllMyRecommend_v2_3",
   "id": {"sid": "jgjycleypyvciibjupanfmbpplyibvsr", "sign": "d41d8cd98f00b204e9800998ecf8427e", "uid": 17139,
          "cVer": "21"},
   "params": [17139, 0, 10]
}

jsonstr = json.dumps(params)
headers = {"Content-type": "application/json", "Accept": "application/json",
          "Connection": "Keep-Alive",
          "User-Agent": "MusicSample/2.4.2 (iPhone; iOS 9.2; Scale/3.00)"}
# use httplib can not  get correct response
res = requests.post(url, data=jsonstr, headers=headers)
print res.text

# use httplib can get correct response
import httplib

httpClient = None
try:
   httpClient = httplib.HTTPConnection("www.demodemo.cc", 80, timeout=30)
   httpClient.request(method="POST", url="/rpc/IUserService", body=jsonstr, headers=headers)

   response = httpClient.getresponse()
   print response.status
   print response.reason
   print response.read()
   print response.getheaders()  # huoqu header
except Exception, e:
   print e
finally:
   if httpClient:
       httpClient.close()



正在回答

0 回答

举报

0/150
提交
取消
初识Python
  • 参与学习       758623    人
  • 解答问题       8667    个

学python入门视频教程,让你快速入门并能编写简单的Python程序

进入课程

同样的参数使用requests库得不到返回结果,而httplib可以正确返回。可能的原因是什么?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信