Urllib2 是否包含有读取长度,求出错原因

2017-06-28 11:28:26 +08:00
 KhadainJHIN
逻辑是读取超长文本( 1w 行),循环读一小部分,数据格式为 json,使用 urllib2 提交 json,read()接受返回数据,写入文件,每次运行读取到固定行的时候既不中断也不写入,Ctrl+c 中断后回显为 data=recv ( 1 )这个
2245 次点击
所在节点    Python
6 条回复
KhadainJHIN
2017-06-28 11:29:24 +08:00
try except 没有起作用...程序没有中断..
KhadainJHIN
2017-06-28 11:33:19 +08:00
查询到结果原因为连接被远端重置...设置 urlopen(timeout)是否有用..另外链接被重置 程序为何没有响应...
bazingaterry
2017-06-28 11:35:54 +08:00
贴代码...
KhadainJHIN
2017-06-28 11:50:44 +08:00
@bazingaterry
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib64/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib64/python2.6/urllib2.py", line 1190, in http_open
return self.do_open( httplib.HTTPConnection, req)
File "/usr/lib64/python2.6/urllib2.py", line 1163, in do_open
r = h.getresponse()
File "/usr/lib64/python2.6/httplib.py", line 990, in getresponse
response.begin()
File "/usr/lib64/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python2.6/httplib.py", line 349, in _read_status
line = self.fp.readline()
File "/usr/lib64/python2.6/socket.py", line 433, in readline
data = recv(1)
KeyboardInterrupt
不能发代码...
KhadainJHIN
2017-06-28 12:00:59 +08:00
@bazingaterry
request = urllib2.Request(url)
while condition :
time = 重连次数
for t in range(time):
try:
time.sleep(0.5)
res = urllib2.urlopen(request,json)
except urllib2.URLError, e:
if t < (time-1):
record_log()
m = res.read()
伪代码..
douxie
2017-06-28 23:38:23 +08:00
表示没看懂你说的啥意思,代码也不全

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/371622

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX