即使curl和浏览器提供连贯的文本,Python请求库也会出现乱码响应

时间:2017-01-19 15:21:01

标签: python api curl

这是我的问题:

1)使用浏览器,我可以下载包含以下内容的文本文件:

URL:protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin = P_6669& Password = XXXXXX

  

HTTP / 1.1 200 OK内容类型:application / json;字符集= UTF-8   内容长度:669连接:关闭

     

{ “ConfigItem”:[{ “CurInciState”: “运行”, “ConfigItemID”: “245767”, “InciStateType”: “运行”, “CurInciStateType”: “运行”, “号码”: “3266000189”, “CreateBy”: “3”, “CurDeplState”: “生产”, “LastVersionID”: “463432”, “DeplState”: “生产”, “CreateTime”:“2017年1月18日   10时09分01" 秒, “DefinitionID”: “466”, “VERSIONID”: “463432”, “DeplStateType”: “生产性”, “CIXMLData”:{ “FilaResponsavel”: “”, “类型”:“主动 - 主动”, “TIPO-inventario”: “”, “VIPaddress”: “sepap.camara.gov.br”, “ID-inventario”: “D0E7907294A0402C9AB0C1AD3D4C4704”, “说明”: “阿泰斯特”, “链接inventario” : “”}, “类”:“群集   基”, “InciState”: “运行”, “CurDeplStateType”: “生产性”, “名称”: “群集-SEPAP”}]}

2)使用curl:

curl -k'协议://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin = P_6669& Password = XXXXXX'

  

{ “机票”:[{ “EscalationTimeWorkingTime”: “ - 382512”, “年龄”:846912 “PriorityID”: “3”, “EscalationDestinationTime”: “1483993892”, “服务ID”: “”,“EscalationDestinationIn “:” 106H   15米 “ ”SolutionTimeWorkingTime“: ” - 357312“, ”类型“: ”未分类“, ”责任“: ”根@本地“, ”SolutionTimeDestinationDate“:” 2017年1月10日   11时31分32" 秒, “STATEID”: “4”, “ResponsibleID”: “1”, “ChangeBy”: “1”, “EscalationTime”: - 843312, “SolutionTimeDestinationTime”: “1484055092”, “EscalationDestinationDate”: “2017年1月9日   18点31分32" 秒, “OWNERID”: “1”, “变更”:“2017年1月9日   17时31分34" 秒, “FirstResponseTimeWorkingTime”: “ - 382512”, “RealTillTimeNotUsed”: “0”, “组ID”: “1”, “所有者”: “根@本地”, “客户id”:“CENTRO   DEINFORMÁTICA“,”TypeID“:”1“,”Created“:”2017-01-09   17时31分32" 秒, “优先级”:“3   正常”, “FirstResponseTimeEscalation”:1, “UntilTime”:0 “EscalationUpdateTime”: “0”, “QueueID”: “1”, “队列”: “邮政”, “国家”: “开放式”, “标题” : “门票#3”, “FirstResponseTimeDestinationDate”:“2017年1月9日   18点31分32" 秒, “CreateBy”: “1”, “TicketID”: “12621”, “StateType”: “开”, “UnlockTimeout”: “0”, “EscalationResponseTime”: “1483993892”, “FirstResponseTimeDestinationTime” : “1483993892”, “EscalationSolutionTime”: “1484055092”, “LockID”: “1”, “ArchiveFlag”: “N”, “TicketNumber”: “30004295”, “SolutionTime”: - 782112, “CreateTimeUnix”:“1483990292 ”, “锁定”: “解锁”, “SolutionTimeEscalation”:1, “SLAID”: “”, “CustomerUserID”: “P_7029”, “FirstResponseTime”: - 843312}]}

3)使用Python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
usuario = {'UserLogin':'P_6669','Password':'XXXXXX'}
headers = {'accept': 'application/json','content-type' : 'applicatio/json'}

ret = requests.get('protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621', verify=False, params=usuario)

print "URL: " + ret.url
print "Status Code: " + str(ret.status_code)
print "Raw: " + str(ret.raw)
try:
        print "Text: " + str(ret.text)
except:
        print "TEXT: ERRO!"

print "Headers: " + str(ret.headers)
try:
        print "Json: " + ret.json()
except:
        print "Json: ERRO!"
print "Content: " + ret.content

Resultado:

URL:protocol://URL/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/12621?UserLogin = P_6669& Password = XXXXXX

状态代码:200

Raw:urllib3.response.HTTPResponse对象位于0x7f8e86628510

TEXT:ERRO!

标题:CaseInsensitiveDict({})

杰森:ERRO!

内容: $ 6 u |A ^ .........这里有很多垃圾....

我已经尝试了很多requests.getrequests.posthttplib2.Http的组合。我总是遇到这个错误。

为什么会这样?

3 个答案:

答案 0 :(得分:2)

这样的事情怎么样:

import gzip
print gzip.decompress(ret.content)

可替换地:

import zlib
print zlib.decompress(ret.content, 16+zlib.MAX_WBITS)

答案 1 :(得分:1)

1)使用我的代码,问题是gzip响应......

适用于

    import zlib
    print zlib.decompress(ret.content, 16+zlib.MAX_WBITS)

2)但是,朋友找到了更好的解决方案:

   #!/usr/bin/env python

   import json
   from pprint import pprint
   from urllib2 import urlopen, Request, URLError, HTTPError

   userdata = {
     'UserLogin': 'p_xxxxx',
     'Password' : 'xxxxx'
   }

   url = 'protocol://url/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST'

   try:
       session_request = Request(url + '/Session', json.dumps(userdata))
       session_id      = urlopen(session_request).read()

       ticket_search   = Request(url + '/Ticket/12621', session_id)
       ticket_response = urlopen(ticket_search).read()

       pprint(json.loads(ticket_response))
   except HTTPError, e:
       print e
   except URLError, e:
       print e.reason

我不知道为什么urllib2.urlopen可以解决和解码gzip并请求不能...

答案 2 :(得分:0)

Accept-Encoding的请求标头中删除“ br”有助于我解决此问题。

headers = {
   "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
   "Accept-Encoding": "gzip, deflate, br",
   "Accept-Language": "en-US,en;q=0.9",
   "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
}