python套接字编程期间http响应体中的奇怪数字

2024-06-01 09:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试向api发送get请求并获得响应。我的代码运行良好,但响应体存在问题,我在响应体前后都得到了数字。这是我的密码:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s = ssl.create_default_context().wrap_socket(s, server_hostname='randomuser.me')
s.connect(("randomuser.me", 443))
request = "GET /api/ HTTP/1.1\r\nHost: randomuser.me\r\n\r\n"
s.sendall(request.encode())

result = ""
while True:
    try:
        data = s.recv(1024)
        result = result + data.decode()
    except: #timeout
        break
    
s.close()
print(result) 

以下是我的回答:

HTTP/1.1 200 OK
Date: Tue, 29 Jun 2021 16:38:41 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
x-powered-by: Express
access-control-allow-origin: *
cache-control: no-cache
etag: W/"4a0-ZHCP6s4BF3NGwp45RPw24DhbGhw"
vary: Accept-Encoding
CF-Cache-Status: DYNAMIC
cf-request-id: 0afa3c3163000038cf7c3e4000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v2?s=fEMqmLbj7EmU8DoZiHR5%2B1uSK29U8clj2yhSA%2FQH%2BhpYdWJzWEGo3ua1Kgh6oHuloggqeZCDgbHTifYw%2FVYiRzgcV8HaIN%2FRHAvjwG1sg%2FH8vrZ5YODwmHwryw%3D%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 6670962f0de338cf-ATH
alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400

4a0
{"results":[{"gender":"female","name":{"title":"Miss","first":"Sophia","last":"Bourgeois"},"location":{"street":{"number":6699,"name":"Rue du Village"},"city":"Orléans","state":"Morbihan","country":"France","postcode":70073,"coordinates":{"latitude":"-2.1449","longitude":"0.6974"},"timezone":{"offset":"-3:00","description":"Brazil, Buenos Aires, Georgetown"}},"email":"sophia.bourgeois@example.com","login":{"uuid":"e145adab-a136-4ef3-b9db-c5bdc3a32a75","username":"sadrabbit387","password":"smooth","salt":"gnz57npL","md5":"81b0e38cc190d63f6ecd275491a1feea","sha1":"b91e19770f3658500d37c11a5bd848c8baafe88e","sha256":"0a8ef17533b064723be96bee5d6338ded189e3184d5b9bdfc15af8243a3b8481"},"dob":{"date":"1984-02-26T02:12:49.648Z","age":37},"registered":{"date":"2016-09-21T08:57:41.436Z","age":5},"phone":"02-62-16-43-97","cell":"06-17-01-09-53","id":{"name":"INSEE","value":"2NNaN80745407 74"},"picture":{"large":"https://randomuser.me/api/portraits/women/76.jpg","medium":"https://randomuser.me/api/portraits/med/women/76.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/76.jpg"},"nat":"FR"}],"info":{"seed":"ad0ba79d620ddb6d","results":1,"page":1,"version":"1.3"}}
0 

除了正文前的数字和正文后的0之外,一切正常。我错过了什么,但是什么?它们是什么?我不熟悉原始http响应

我还检查了postman和curl,他们的回答很好,他们的回答中没有这样的数字。这与api无关,因为我在一些不同的api中得到了“7ac5”类型的数字。然而,有些API工作得很好。我不懂,我想学

编辑:

另一个具有不同api的示例

HTTP/1.1 200 OK
Date: Tue, 29 Jun 2021 17:06:59 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Cache-Control: max-age=100, public
CF-Cache-Status: DYNAMIC
cf-request-id: 0afa5616e5000054823eb60000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 6670bf9e3c545482-IST
alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400

bcd
{"id":"btc-bitcoin","name":"Bitcoin","symbol":"BTC","rank":1,"is_new":false,"is_active":true,"type":"coin","tags":[{"id":"segwit","name":"Segwit","coin_counter":10,"ico_counter":0},{"id":"cryptocurrency","name":"Cryptocurrency","coin_counter":782,"ico_counter":40},{"id":"proof-of-work","name":"Proof Of Work","coin_counter":425,"ico_counter":15},{"id":"payments","name":"Payments","coin_counter":185,"ico_counter":39},{"id":"sha256","name":"Sha256","coin_counter":45,"ico_counter":1},{"id":"mining","name":"Mining","coin_counter":300,"ico_counter":18},{"id":"lightning-network","name":"Lightning Network","coin_counter":7,"ico_counter":0}],"team":[{"id":"satoshi-nakamoto","name":"Satoshi Nakamoto","position":"Founder"},{"id":"wladimir-j-van-der-laan","name":"Wladimir J. van der Laan","position":"Blockchain Developer"},{"id":"jonas-schnelli","name":"Jonas Schnelli","position":"Blockchain Developer"},{"id":"marco-falke","name":"Marco Falke","position":"Blockchain Developer"}],"description":"Bitcoin is a cryptocurrency and worldwide payment system. It is the first decentralized digital currency, as the system works without a central bank or single administrator.","message":"","open_source":true,"started_at":"2009-01-03T00:00:00Z","development_status":"Working product","hardware_wallet":true,"proof_type":"Proof of Work","org_structure":"Decentralized","hash_algorithm":"SHA256","links":{"explorer":["https://blockchair.com/bitcoin","http://blockchain.com/explorer","https://blockstream.info/","https://live.blockcypher.com/btc/","https://btc.cryptoid.info/btc/"],"facebook":["https://www.facebook.com/bitcoins/"],"reddit":["https://www.reddit.com/r/bitcoin"],"source_code":["https://github.com/bitcoin/bitcoin"],"website":["https://bitcoin.org/"],"youtube":["https://www.youtube.com/watch?v=Gc2en3nHxA4\u0026"]},"links_extended":[{"url":"https://bitcoin.org/en/blog","type":"blog"},{"url":"https://blockchair.com/bitcoin","type":"explorer"},{"url":"http://blockchain.com/explorer","type":"explorer"},{"url":"https://blockstream.info/","type":"explorer"},{"url":"https://live.blockcypher.com/btc/","type":"explorer"},{"url":"https://btc.cryptoid.info/btc/","type":"explorer"},{"url":"https://www.facebook.com/bitcoins/","type":"facebook"},{"url":"https://bitcointalk.org","type":"message_board"},{"url":"https://www.reddit.com/r/bitcoin","type":"reddit","stats":{"subscribers":3145581}},{"url":"https://github.com/bitcoin/bitcoin","type":"source_code","stats":{"contributors":975,"stars":55314}},{"url":"https://twitter.com/bitcoincoreorg","type":"twitter","stats":{"followers":126039}},{"url":"https://electrum.org/#download","type":"wallet"},{"url":"https://bitcoin.org/","type":"website"},{"url":"https://www.youtube.com/watch?v=Gc2en3nHxA4\u0026","type":"youtube"}],"whitepaper":{"link":"https://static.coinpaprika.com/storage/cdn/whitepapers/215.pdf","thumbnail":"https://static.coinpaprika.com/storage/cdn/whitepapers/217.jpg"},"first_data_at":"2010-07-17T00:00:00Z","last_data_at":"2021-06-29T17:05:00Z"}
0

Tags: namehttpscomapiidurlagetype
1条回答
网友
1楼 · 发布于 2024-06-01 09:17:37

I am missing something but what?

您没有考虑到这些响应正在使用chunked传输编码格式(通过Transfer-Encoding: chunked头)以块的形式发送正文数据,而不是像您所期望的那样以单字节流的形式发送正文数据。有关chunked格式的更多详细信息,请参见RFC 2616 Section 3.6.1RFC 7230 Section 4.1

What are they?

您所指的数字是块大小指示器

  • 在显示的第一个响应中,有一个字节大小为4a0(0x4A0十六进制,1184十进制)的数据块,后面是一个字节大小为0的终止块

  • 在显示的第二个响应中,有一个字节大小为bcd(0xBCD十六进制,3021十进制)的数据块,后面是一个字节大小为0的终止块

长度为0的块结束正文数据(不存在Content-LengthConnection: close头以其他方式结束响应)

您将无法使用简单的recv()循环来读取chunked实体。您必须检测chunked头,如果存在,则分别读取和解析每个块。读取块大小,跳至以下CRLF,读取指定的字节数,跳至以下CRLF。重复此操作,直到达到0长度的区块。然后读取块后面的一组后续HTTP头,覆盖在正文之前读取的任何头

请参阅我在this answerthis answer中提供的伪代码

相关问题 更多 >