python reptile series: Three, URLError exception handling

1.URLError

First, to explain why under URLError may arise:

  • No network connection, that the machine can not access
  • Not connect specific server
  • The server does not exist

In the code, we need to use try-except statement to surround and capture the corresponding exception.

2.HTTPError

HTTPError is a subclass of URLError, when you use urlopen method sends a request to the server in the response object corresponding to a Response, wherein it comprises a number "status code."

Status code will be summarized as follows:

100: The client continues to send the request should continue. The client should continue the remainder of the transmission request, or if the request has been completed, the response is ignored.

101: protocol conversion After transmitting the response to the last empty row, the server will switch to that protocol as defined in the Upgrade message header. Similar measures should be taken only when switching the new agreement is more beneficial.

102: continue processing spread by WebDAV (RFC 2518) status code representative process will continue to be executed.

200: request was successful treatment: the content of the response is obtained, the processing

201: the request is complete, the result is the creation of new resources. The newly created resource URI can get treatment in the entity of the response: reptile will not encounter

202: request is accepted, but the process has not been completed treatment: blocking wait

204: The server has fulfilled the request, but did not return the new information. If the client is a user agent is not required for the update its document view. Treatment: discards

300: The status code is not used directly to HTTP / 1.0 application, just as the default interpretation type 3XX response. There are a plurality of the requested resources are available. Treatment: If the program can be processed, the further processing, if the program can not be processed is discarded
301: request to the resource is assigned a permanent URL, so that you can access this resource handling by the URL in the future: redirected to the assigned URL

302: resource request to the URL in a temporary storage different treatment: a temporary URL to redirect

304: The requested resource is not the update processing mode: to discard

400: illegal request handling: discards

401:未授权     处理方式:丢弃

403:禁止     处理方式:丢弃

404:没有找到     处理方式:丢弃

500:服务器内部错误  服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理。一般来说,这个问题都会在服务器端的源代码出现错误时出现。

501:服务器无法识别  服务器不支持当前请求所需要的某个功能。当服务器无法识别请求的方法,并且无法支持其对任何资源的请求。

502:错误网关  作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应。

503:服务出错   由于临时的服务器维护或者过载,服务器当前无法处理请求。这个状况是临时的,并且将在一段时间以后恢复。

HTTPError实例产生后会有一个code属性,这就是是服务器发送的相关错误号。
因为urllib可以为你处理重定向,也就是3开头的代号可以被处理,并且100-299范围的号码指示成功,所以你只能看到400-599的错误号码。

  

我们知道,HTTPError的父类是URLError,根据编程经验,父类的异常应当写到子类异常的后面,如果子类捕获不到,那么可以捕获父类的异常,所以上述的代码可以这么改写 

如果捕获到了HTTPError,则输出code,不会再处理URLError异常。如果发生的不是HTTPError,则会去捕获URLError异常,输出错误原因。

另外还可以加入 hasattr属性提前对属性进行判断,代码改写如下

首先对异常的属性进行判断,以免出现属性输出报错的现象。

以上,就是对URLError和HTTPError的相关介绍,以及相应的错误处理办法。

 

 

 

Guess you like

Origin www.cnblogs.com/biao/p/11983067.html