Python论坛  - 讨论区

标题:Re: Re: Re:[python-chinese]ÔõôµÃµ½http libµÄÒì³£ÄØ?

2005年02月22日 星期二 09:18

hw_cg hw_cg at huawei.com
Tue Feb 22 09:18:53 HKT 2005

ftplib中的all_error是它这个模块所有可能抛出的异常的tuple.
不清楚为什么httplib中没有这个定义,也许httplib与ftplib不是一个人开发的吧,呵呵.


如果你也就是想要all_errors这个定义,照葫芦画瓢,定义自己的all_errors即可.
all_error = ( socket.error, IOError, EOFError,  httplib.HTTPException, Exception )
使用:
except all_error , msg:
    print msg

只是这样写好像没有太大的意义,如果你没有必要对异常进行分类处理,就不要区分异常类型了.
使用
因为为了防止遗漏,我还是把 Exception 加入到all_error tuple里了.



----- Original Message ----- 
From: "cry" <zyqmail at 163.net>
To: <python-chinese at lists.python.cn>
Sent: Monday, February 21, 2005 7:58 PM
Subject: Re: Re: Re:[python-chinese]ÔõôµÃµ½http libµÄÒì³£ÄØ?


hw_cg,您好!

谢谢,你这样一说,我清楚了很多。
但是ftplib为什么做了一个all_error,httplib就没有呢?

在 2005-02-21 14:21:00 您写道:
>cry: 你好!
>
>借鉴其他语言的异常思想,不是这样写捕获语句的.
>你的捕获机制不完善,导致你漏掉了一些异常.
>
>建议你的异常捕获象下面这样分级捕获, 每个异常捕获语句仅捕获自己感兴趣的异常,
>最后使用Exception捕获所有未拦截的异常.
>
>import httplib
>import socket
>
>try:
>    conn = httplib.HTTPConnection("127.0.0.1:8080")
>    conn.request("GET", "/")
>    response = conn.getresponse()
>    result = response.read()
>    print result
>    conn.close()
>
>except socket.error, msg:
>    print msg
>    # do something other
>
>except httplib.HTTPException,msg:
>    print msg
>    # do something other
>
>except Exception, msg:
>    print msg
>    # do something other
>
>
>
>
>----- Original Message ----- 
>From: "cry" <zyqmail at 163.net>
>To: "limodou, python-chinese at lists.python.cn" <limodou at gmail.com,python-chinese at lists.python.cn>
>Sent: Monday, February 21, 2005 1:26 PM
>Subject: Re: Re: [python-chinese] ???′μ?μ?httplib μ?òì3£??£?
>
>
>limodou,您好!
>
>直接用except:当然是没有问题,但是我需要错误信息呀,比如:
>except error,msg:
>
>
>在 2005-02-21 12:22:00 您写道:
>>捕获Exception 不要httplib.HTTPException 试试
>>
>>
>>On Mon, 21 Feb 2005 12:5:36 +0800, cry <zyqmail at 163.net> wrote:
>>> python,您好!
>>> 
>>> 我做了如下的代码:
>>> try:
>>>         conn = httplib.HTTPConnection("192.168.1.103")
>>>         conn.request("GET", "/")
>>>         response = conn.getresponse()
>>>         result = response.read()
>>>         conn.close()
>>>  except httplib.HTTPException , msg:
>>>         print msg
>>> 
>>> 运行后:
>>> Traceback (most recent call last):
>>>   File "", line 3, in ?
>>>   File "I:\Python23\lib\httplib.py", line 718, in request
>>>     self._send_request(method, url, body, headers)
>>>   File "I:\Python23\lib\httplib.py", line 739, in _send_request
>>>     self.endheaders()
>>>   File "I:\Python23\lib\httplib.py", line 712, in endheaders
>>>     self._send_output()
>>>   File "I:\Python23\lib\httplib.py", line 597, in _send_output
>>>     self.send(msg)
>>>   File "I:\Python23\lib\httplib.py", line 564, in send
>>>     self.connect()
>>>   File "I:\Python23\lib\httplib.py", line 532, in connect
>>>     socket.SOCK_STREAM):
>>> error: (10060, 'Operation timed out')
>>> 
>>> 错在那里呢?我怎么能控制httplib的exception呢?
>>> 我这样处理ftplib就可以。
>>> 
>>> 谢谢。
>>> 
>>>>>> 礼!
>>> 
>>>             cry
>>>             zyqmail at 163.net
>>> 
>>> _______________________________________________
>>> python-chinese list
>>> python-chinese at lists.python.cn
>>> http://python.cn/mailman/listinfo/python-chinese
>>> 
>>
>>
>>-- 
>>I like python! 
>>My Blog: http://www.donews.net/limodou
>>New Maillist: http://groups-beta.google.com/group/python-cn
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>
>>礼!
>
>            cry
>            zyqmail at 163.net
>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

                    致
礼!

            cry
            zyqmail at 163.net


_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号