Hướng dẫn python url scheme - lược đồ url python

Tác giả

Michael Foord

Giới thiệu¶

Urllib.Request là một mô -đun Python để tìm nạp các URL [bộ định vị tài nguyên thống nhất]. Nó cung cấp một giao diện rất đơn giản, dưới dạng chức năng Urlopen. Điều này có khả năng tìm nạp các URL bằng nhiều giao thức khác nhau. Nó cũng cung cấp một giao diện phức tạp hơn một chút để xử lý các tình huống phổ biến - như xác thực cơ bản, cookie, proxy, v.v. Chúng được cung cấp bởi các đối tượng gọi là trình xử lý và bộ mở. is a Python module for fetching URLs [Uniform Resource Locators]. It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. These are provided by objects called handlers and openers.

Urllib.Request hỗ trợ tìm nạp các URL cho nhiều sơ đồ URL của Google [được xác định bởi chuỗi trước

import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
5 trong URL - ví dụ
import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
6 là sơ đồ URL của
import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
7] bằng cách sử dụng các giao thức mạng liên quan của chúng [ví dụ: FTP, HTTP]. Hướng dẫn này tập trung vào trường hợp phổ biến nhất, HTTP.

Đối với các tình huống đơn giản, Urlopen rất dễ sử dụng. Nhưng ngay khi bạn gặp lỗi hoặc các trường hợp không tầm thường khi mở URL HTTP, bạn sẽ cần một số hiểu biết về giao thức chuyển siêu văn bản. Tài liệu tham khảo toàn diện và có thẩm quyền nhất về HTTP là RFC 2616. Đây là một tài liệu kỹ thuật và không có ý định dễ đọc. Howto này nhằm mục đích minh họa bằng cách sử dụng urllib, với đủ chi tiết về HTTP để giúp bạn vượt qua. Nó không nhằm thay thế các tài liệu

import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
8, nhưng là bổ sung cho chúng.RFC 2616. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib, with enough detail about HTTP to help you through. It is not intended to replace the
import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
8 docs, but is supplementary to them.

Lấy URLS urls¶

Cách đơn giản nhất để sử dụng urllib.request như sau:

import urllib.request
with urllib.request.urlopen['//python.org/'] as response:
   html = response.read[]

Nếu bạn muốn truy xuất tài nguyên qua URL và lưu trữ nó ở vị trí tạm thời, bạn có thể làm như vậy thông qua các chức năng

import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass
9 và
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
0:

import shutil
import tempfile
import urllib.request

with urllib.request.urlopen['//python.org/'] as response:
    with tempfile.NamedTemporaryFile[delete=False] as tmp_file:
        shutil.copyfileobj[response, tmp_file]

with open[tmp_file.name] as html:
    pass

Nhiều cách sử dụng urllib sẽ đơn giản như vậy [lưu ý rằng thay vì ‘ url, chúng tôi có thể đã sử dụng một URL bắt đầu với‘ ftp: Hồi, ‘tệp:, v.v.]. Tuy nhiên, mục đích của hướng dẫn này để giải thích các trường hợp phức tạp hơn, tập trung vào HTTP.

HTTP dựa trên các yêu cầu và phản hồi - khách hàng thực hiện các yêu cầu và máy chủ gửi phản hồi. Urllib.Request phản ánh điều này với một đối tượng

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
1 đại diện cho yêu cầu HTTP bạn đang thực hiện. Ở dạng đơn giản nhất, bạn tạo một đối tượng yêu cầu chỉ định URL bạn muốn tìm nạp. Gọi
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
2 với đối tượng yêu cầu này trả về một đối tượng phản hồi cho URL được yêu cầu. Phản hồi này là một đối tượng giống như tệp, có nghĩa là bạn có thể gọi
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
3 trên phản hồi:

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]

Lưu ý rằng Urllib.Request sử dụng cùng một giao diện yêu cầu để xử lý tất cả các sơ đồ URL. Ví dụ: bạn có thể thực hiện yêu cầu FTP như vậy:

req = urllib.request.Request['ftp://example.com/']

Trong trường hợp của HTTP, có hai điều bổ sung yêu cầu các đối tượng cho phép bạn thực hiện: Đầu tiên, bạn có thể truyền dữ liệu được gửi đến máy chủ. Thứ hai, bạn có thể chuyển thêm thông tin [siêu dữ liệu] về dữ liệu hoặc về chính yêu cầu, cho máy chủ - thông tin này được gửi dưới dạng tiêu đề HTTP. Hãy cùng nhau nhìn vào từng người trong số này.

Dữ liệu¶

Đôi khi bạn muốn gửi dữ liệu đến URL [thường là URL sẽ đề cập đến tập lệnh CGI [Giao diện cổng thông thường] hoặc ứng dụng web khác]. Với HTTP, điều này thường được thực hiện bằng cách sử dụng những gì được gọi là yêu cầu POST. Đây thường là những gì trình duyệt của bạn làm khi bạn gửi biểu mẫu HTML mà bạn đã điền trên web. Không phải tất cả các bài đăng phải đến từ các biểu mẫu: Bạn có thể sử dụng một bài đăng để truyền dữ liệu tùy ý đến ứng dụng của riêng bạn. Trong trường hợp phổ biến của các biểu mẫu HTML, dữ liệu cần được mã hóa theo cách tiêu chuẩn và sau đó được chuyển đến đối tượng yêu cầu dưới dạng đối số

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
4. Việc mã hóa được thực hiện bằng cách sử dụng một hàm từ thư viện
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
5.POST request. This is often what your browser does when you submit a HTML form that you filled in on the web. Not all POSTs have to come from forms: you can use a POST to transmit arbitrary data to your own application. In the common case of HTML forms, the data needs to be encoded in a standard way, and then passed to the Request object as the
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
4 argument. The encoding is done using a function from the
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
5 library.

import urllib.parse
import urllib.request

url = '//www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }

data = urllib.parse.urlencode[values]
data = data.encode['ascii'] # data should be bytes
req = urllib.request.Request[url, data]
with urllib.request.urlopen[req] as response:
   the_page = response.read[]

Lưu ý rằng các mã hóa khác đôi khi được yêu cầu [ví dụ: để tải lên tệp từ các biểu mẫu HTML - xem Thông số kỹ thuật HTML, gửi biểu mẫu để biết thêm chi tiết].

Nếu bạn không vượt qua đối số

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
4, Urllib sử dụng yêu cầu GET. Một cách mà các yêu cầu nhận và đăng khác nhau là các yêu cầu POS đến cửa của bạn]. Mặc dù tiêu chuẩn HTTP cho thấy rõ rằng các bài đăng được dự định luôn gây ra tác dụng phụ và nhận được các yêu cầu không bao giờ gây ra tác dụng phụ, không có gì ngăn chặn yêu cầu có tác dụng phụ và cũng như không có yêu cầu không có tác dụng phụ. Dữ liệu cũng có thể được truyền trong yêu cầu HTTP nhận bằng cách mã hóa nó trong chính URL.GET request. One way in which GET and POST requests differ is that POST requests often have “side-effects”: they change the state of the system in some way [for example by placing an order with the website for a hundredweight of tinned spam to be delivered to your door]. Though the HTTP standard makes it clear that POSTs are intended to always cause side-effects, and GET requests never to cause side-effects, nothing prevents a GET request from having side-effects, nor a POST requests from having no side-effects. Data can also be passed in an HTTP GET request by encoding it in the URL itself.

Điều này được thực hiện như sau:

>>> import urllib.request
>>> import urllib.parse
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.parse.urlencode[data]
>>> print[url_values]  # The order may differ from below.  
name=Somebody+Here&language=Python&location=Northampton
>>> url = '//www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib.request.urlopen[full_url]

Lưu ý rằng URL đầy đủ được tạo bằng cách thêm

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
7 vào URL, theo sau là các giá trị được mã hóa.

Xử lý các trường hợp ngoại lệ

Urlopen tăng

import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
8 khi nó không thể xử lý phản hồi [mặc dù như thường lệ với API Python, các trường hợp ngoại lệ tích hợp như
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
9,
req = urllib.request.Request['ftp://example.com/']
0, v.v. cũng có thể được nâng lên].

req = urllib.request.Request['ftp://example.com/']
1 là lớp con của
import urllib.request

req = urllib.request.Request['//www.voidspace.org.uk']
with urllib.request.urlopen[req] as response:
   the_page = response.read[]
8 được nêu trong trường hợp cụ thể của URL HTTP.

Các lớp ngoại lệ được xuất từ ​​mô -đun

req = urllib.request.Request['ftp://example.com/']
3.

Urlerror¶

Thông thường, urlerror được nâng lên vì không có kết nối mạng [không có tuyến đường đến máy chủ được chỉ định] hoặc máy chủ được chỉ định không tồn tại. Trong trường hợp này, ngoại lệ được nâng lên sẽ có thuộc tính ‘lý do, đây là một bộ xử lý có chứa mã lỗi và thông báo lỗi văn bản.

e.g.

>>> req = urllib.request.Request['//www.pretend_server.org']
>>> try: urllib.request.urlopen[req]
... except urllib.error.URLError as e:
...     print[e.reason]      
...
[4, 'getaddrinfo failed']

Lỗi HTTP¶

Mỗi phản hồi HTTP từ máy chủ đều chứa mã trạng thái số. Đôi khi mã trạng thái chỉ ra rằng máy chủ không thể đáp ứng yêu cầu. Các trình xử lý mặc định sẽ xử lý một số phản hồi này cho bạn [ví dụ: nếu phản hồi là chuyển hướng của Cameron, yêu cầu máy khách lấy tài liệu từ một URL khác, Urllib sẽ xử lý điều đó cho bạn]. Đối với những người có thể xử lý, Urlopen sẽ tăng

req = urllib.request.Request['ftp://example.com/']
1. Các lỗi điển hình bao gồm ‘404, [không tìm thấy trang],‘ 403, [Yêu cầu bị cấm] và ‘401 [yêu cầu xác thực].

Xem Phần 10 của RFC 2616 để biết tham khảo trên tất cả các mã lỗi HTTP.RFC 2616 for a reference on all the HTTP error codes.

Ví dụ

req = urllib.request.Request['ftp://example.com/']
1 được nâng lên sẽ có thuộc tính mã số nguyên, tương ứng với lỗi được gửi bởi máy chủ.

Mã lỗi

Vì các trình xử lý mặc định xử lý chuyển hướng [mã trong phạm vi 300] và mã trong phạm vi 10029299 cho thấy thành công, bạn thường sẽ chỉ thấy mã lỗi trong phạm vi 400 Lỗi599.

req = urllib.request.Request['ftp://example.com/']
6 là một từ điển hữu ích của các mã phản hồi trong đó cho thấy tất cả các mã phản hồi được sử dụng bởi RFC 2616. Từ điển được sao chép ở đây để thuận tiệnRFC 2616. The dictionary is reproduced here for convenience

# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }

Khi một lỗi được nêu ra, máy chủ sẽ phản hồi bằng cách trả về mã lỗi HTTP và trang lỗi. Bạn có thể sử dụng thể hiện

req = urllib.request.Request['ftp://example.com/']
1 làm phản hồi trên trang được trả về. Điều này có nghĩa là cũng như thuộc tính mã, nó cũng có các phương thức đọc, geturl và thông tin, được trả về bởi mô -đun
req = urllib.request.Request['ftp://example.com/']
8:

>>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.pretend_server.org']
>>> try: urllib.request.urlopen[req]
... except urllib.error.URLError as e:
...     print[e.reason]      
...
[4, 'getaddrinfo failed']
2 của chúng tôi cho
>>> import urllib.request
>>> import urllib.parse
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.parse.urlencode[data]
>>> print[url_values]  # The order may differ from below.  
name=Somebody+Here&language=Python&location=Northampton
>>> url = '//www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib.request.urlopen[full_url]
4. Theo mặc định, các trình mở có trình xử lý cho các tình huống thông thường -
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
2 [nếu cài đặt proxy như biến môi trường
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
3 được đặt],
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
4,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
5,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
6,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
7,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
8,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
9,
>>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.pretend_server.org']
>>> try: urllib.request.urlopen[req]
... except urllib.error.URLError as e:
...     print[e.reason]      
...
[4, 'getaddrinfo failed']
2 của chúng tôi cho
>>> import urllib.request
>>> import urllib.parse
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.parse.urlencode[data]
>>> print[url_values]  # The order may differ from below.  
name=Somebody+Here&language=Python&location=Northampton
>>> url = '//www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib.request.urlopen[full_url]
4. Theo mặc định, các trình mở có trình xử lý cho các tình huống thông thường -
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
2 [nếu cài đặt proxy như biến môi trường
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
3 được đặt],
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
4,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
5,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
6,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
7,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
8,
# Table mapping response codes to messages; entries have the
# form {code: [shortmessage, longmessage]}.
responses = {
    100: ['Continue', 'Request received, please continue'],
    101: ['Switching Protocols',
          'Switching to new protocol; obey Upgrade header'],

    200: ['OK', 'Request fulfilled, document follows'],
    201: ['Created', 'Document created, URL follows'],
    202: ['Accepted',
          'Request accepted, processing continues off-line'],
    203: ['Non-Authoritative Information', 'Request fulfilled from cache'],
    204: ['No Content', 'Request fulfilled, nothing follows'],
    205: ['Reset Content', 'Clear input form for further input.'],
    206: ['Partial Content', 'Partial content follows.'],

    300: ['Multiple Choices',
          'Object has several resources -- see URI list'],
    301: ['Moved Permanently', 'Object moved permanently -- see URI list'],
    302: ['Found', 'Object moved temporarily -- see URI list'],
    303: ['See Other', 'Object moved -- see Method and URL list'],
    304: ['Not Modified',
          'Document has not changed since given time'],
    305: ['Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'],
    307: ['Temporary Redirect',
          'Object moved temporarily -- see URI list'],

    400: ['Bad Request',
          'Bad request syntax or unsupported method'],
    401: ['Unauthorized',
          'No permission -- see authorization schemes'],
    402: ['Payment Required',
          'No payment -- see charging schemes'],
    403: ['Forbidden',
          'Request forbidden -- authorization will not help'],
    404: ['Not Found', 'Nothing matches the given URI'],
    405: ['Method Not Allowed',
          'Specified method is invalid for this server.'],
    406: ['Not Acceptable', 'URI not available in preferred format.'],
    407: ['Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'],
    408: ['Request Timeout', 'Request timed out; try again later.'],
    409: ['Conflict', 'Request conflict.'],
    410: ['Gone',
          'URI no longer exists and has been permanently removed.'],
    411: ['Length Required', 'Client must specify Content-Length.'],
    412: ['Precondition Failed', 'Precondition in headers is false.'],
    413: ['Request Entity Too Large', 'Entity is too large.'],
    414: ['Request-URI Too Long', 'URI is too long.'],
    415: ['Unsupported Media Type', 'Entity body in unsupported format.'],
    416: ['Requested Range Not Satisfiable',
          'Cannot satisfy request range.'],
    417: ['Expectation Failed',
          'Expect condition could not be satisfied.'],

    500: ['Internal Server Error', 'Server got itself in trouble'],
    501: ['Not Implemented',
          'Server does not support this operation'],
    502: ['Bad Gateway', 'Invalid responses from another server/proxy.'],
    503: ['Service Unavailable',
          'The server cannot process the request due to a high load'],
    504: ['Gateway Timeout',
          'The gateway server did not receive a timely response'],
    505: ['HTTP Version Not Supported', 'Cannot fulfill request.'],
    }
9,
>>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n>> req = urllib.request.Request['//www.python.org/fish.html']
>>> try:
...     urllib.request.urlopen[req]
... except urllib.error.HTTPError as e:
...     print[e.code]
...     print[e.read[]]  
...
404
b'\n\n\n

Bài Viết Liên Quan

Chủ Đề