【发布时间】:2015-12-02 09:45:23
【问题描述】:
这对我学习如何进行低级套接字通信来说是非常困难的一步,但我真的很想学习这个,我遇到了困难,我似乎无法找到正确的方式。
如何获取所有数据?我已经尝试了多种方法,但只能获得部分响应。
我现在正在尝试的网址是:
http://steamcommunity.com/market/search/render/?query=&start=0&count=100&search_descriptions=0&sort_column=price&sort_dir=asc&appid=730&category_730_ItemSet%5B%5D=any&category_730_ProPlayer%5B%5D=any&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D=any&category_730_Rarity%5B%5D=tag_Rarity_Ancient_Weapon
经过研究,我尝试了这种方式,但仍然无法打印上面的完整 JSON 页面,我做错了什么吗?
sock.send(request)
response = ""
first = True
length = 0
while True:
partialResponse = sock.recv(65536)
if len(partialResponse) != 0:
#print("all %s" % partialResponse)
# Extract content length from the first chunk
if first:
startPosition = partialResponse.find("Content-Length")
if startPosition != -1:
endPosition = partialResponse.find("\r\n", startPosition+1)
length = int(partialResponse[startPosition:endPosition].split(" ")[1])
first = False
# add current chunk to entire content
response += partialResponse
# remove chunksize from chunck
startPosition = response.find("\n0000")
if startPosition != -1:
endPosition = response.find("\n", startPosition+1)
response = response[0:startPosition-1] + response[endPosition+1:]
if len(response[response.find("\r\n\r\n")+2:]) > length:
break
else:
break
print response
【问题讨论】:
-
如果你想要整个东西,你为什么要求最大 64K 字节?
-
我应该要求的最大值是多少?
-
@ryachza 好吧,我已经尝试过打印对日志的响应,但每次响应都比 chrome 浏览器上显示的要短。
-
嗯。就我而言 Brainfart - 考虑文件读取 :)
标签: python python-2.7 sockets python-sockets