V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
KhadainJHIN
V2EX  ›  Python

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

  •  
  •   KhadainJHIN · 2017-06-28 11:28:26 +08:00 · 2257 次点击
    这是一个创建于 2712 天前的主题,其中的信息可能已经有所发展或是发生改变。
    逻辑是读取超长文本( 1w 行),循环读一小部分,数据格式为 json,使用 urllib2 提交 json,read()接受返回数据,写入文件,每次运行读取到固定行的时候既不中断也不写入,Ctrl+c 中断后回显为 data=recv ( 1 )这个
    6 条回复    2017-06-28 23:38:23 +08:00
    KhadainJHIN
        1
    KhadainJHIN  
    OP
       2017-06-28 11:29:24 +08:00
    try except 没有起作用...程序没有中断..
    KhadainJHIN
        2
    KhadainJHIN  
    OP
       2017-06-28 11:33:19 +08:00
    查询到结果原因为连接被远端重置...设置 urlopen(timeout)是否有用..另外链接被重置 程序为何没有响应...
    bazingaterry
        3
    bazingaterry  
       2017-06-28 11:35:54 +08:00 via iPhone
    贴代码...
    KhadainJHIN
        4
    KhadainJHIN  
    OP
       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
        5
    KhadainJHIN  
    OP
       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
        6
    douxie  
       2017-06-28 23:38:23 +08:00
    表示没看懂你说的啥意思,代码也不全
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1038 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:18 · PVG 07:18 · LAX 15:18 · JFK 18:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.