Python異常處理

來源:互聯網
上載者:User

標籤:產生   use   發送   .net   urllib2   perror   pen   nbsp   color   

1、URLError

錯誤產生原因:

  • 網路無串連,即本機無法上網
  • 串連不到特定的伺服器
  • 伺服器不存在
try:except 樣本:
import urllib2 requset = urllib2.Request(‘http://www.xxxxx.com‘)try:urllib2.urlopen(request)except urllib2.URLError, e:print e.reason

 

2、HTTPErrorHTTPError執行個體產生後會有一個code屬性,這就是是伺服器發送的相關錯誤號碼。因為urllib2可以為你處理重新導向,也就是3開頭的代號可以被處理,並且100-299範圍的號碼指示成功,所以你只能看到400-599的錯誤號碼。 
req = urllib2.Request(‘http://blog.csdn.net/cqcre‘)try:    urllib2.urlopen(req)except urllib2.HTTPError, e:    print e.codeexcept urllib2.URLError, e:    print e.reasonelse:    print "OK"

 

樣本中涉及Request方法,關於此方法使用可自行查詢。

  

Python異常處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.