Changeset 391 for python/trunk/Doc/library/httplib.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/httplib.rst
r2 r391 7 7 .. note:: 8 8 The :mod:`httplib` module has been renamed to :mod:`http.client` in Python 9 3. 0.The :term:`2to3` tool will automatically adapt imports when converting10 your sources to 3.0.9 3. The :term:`2to3` tool will automatically adapt imports when converting 10 your sources to Python 3. 11 11 12 12 … … 17 17 .. index:: module: urllib 18 18 19 **Source code:** :source:`Lib/httplib.py` 20 21 -------------- 22 19 23 This module defines classes which implement the client side of the HTTP and 20 24 HTTPS protocols. It is normally not used directly --- the module :mod:`urllib` … … 35 39 36 40 37 .. class:: HTTPConnection(host[, port[, strict[, timeout ]]])41 .. class:: HTTPConnection(host[, port[, strict[, timeout[, source_address]]]]) 38 42 39 43 An :class:`HTTPConnection` instance represents one transaction with an HTTP … … 47 51 operations (like connection attempts) will timeout after that many seconds 48 52 (if it is not given, the global default timeout setting is used). 53 The optional *source_address* parameter may be a tuple of a (host, port) 54 to use as the source address the HTTP connection is made from. 49 55 50 56 For example, the following calls all create instances that connect to the server … … 61 67 *timeout* was added. 62 68 63 64 .. class:: HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout]]]]]) 69 .. versionchanged:: 2.7 70 *source_address* was added. 71 72 73 .. class:: HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address]]]]]]) 65 74 66 75 A subclass of :class:`HTTPConnection` that uses SSL for communication with … … 69 78 certificate chain file. 70 79 71 .. note:: 72 73 This does not do any certificate verification. 80 .. warning:: 81 This does not do any verification of the server's certificate. 74 82 75 83 .. versionadded:: 2.0 … … 78 86 *timeout* was added. 79 87 80 81 .. class:: HTTPResponse(sock[, debuglevel=0][, strict=0]) 88 .. versionchanged:: 2.7 89 *source_address* was added. 90 91 92 .. class:: HTTPResponse(sock, debuglevel=0, strict=0) 82 93 83 94 Class whose instances are returned upon successful connection. Not instantiated … … 85 96 86 97 .. versionadded:: 2.0 98 99 .. class:: HTTPMessage 100 101 An :class:`HTTPMessage` instance is used to hold the headers from an HTTP 102 response. It is implemented using the :class:`mimetools.Message` class and 103 provides utility functions to deal with HTTP Headers. It is not directly 104 instantiated by the users. 105 87 106 88 107 The following exceptions are raised as appropriate: … … 429 448 430 449 450 .. method:: HTTPConnection.set_tunnel(host,port=None, headers=None) 451 452 Set the host and the port for HTTP Connect Tunnelling. Normally used when 453 it is required to do HTTPS Conection through a proxy server. 454 455 The headers argument should be a mapping of extra HTTP headers to send 456 with the CONNECT request. 457 458 .. versionadded:: 2.7 459 460 431 461 .. method:: HTTPConnection.connect() 432 462 … … 463 493 464 494 465 .. method:: HTTPConnection.endheaders() 466 467 Send a blank line to the server, signalling the end of the headers. 495 .. method:: HTTPConnection.endheaders(message_body=None) 496 497 Send a blank line to the server, signalling the end of the headers. The 498 optional *message_body* argument can be used to pass a message body 499 associated with the request. The message body will be sent in the same 500 packet as the message headers if it is string, otherwise it is sent in a 501 separate packet. 502 503 .. versionchanged:: 2.7 504 *message_body* was added. 468 505 469 506 … … 500 537 .. versionadded:: 2.4 501 538 539 .. method:: HTTPResponse.fileno() 540 541 Returns the ``fileno`` of the underlying socket. 502 542 503 543 .. attribute:: HTTPResponse.msg … … 542 582 >>> conn.close() 543 583 584 Here is an example session that uses the ``HEAD`` method. Note that the 585 ``HEAD`` method never returns any data. :: 586 587 >>> import httplib 588 >>> conn = httplib.HTTPConnection("www.python.org") 589 >>> conn.request("HEAD","/index.html") 590 >>> res = conn.getresponse() 591 >>> print res.status, res.reason 592 200 OK 593 >>> data = res.read() 594 >>> print len(data) 595 0 596 >>> data == '' 597 True 598 544 599 Here is an example session that shows how to ``POST`` requests:: 545 600 546 601 >>> import httplib, urllib 547 >>> params = urllib.urlencode({' spam': 1, 'eggs': 2, 'bacon': 0})602 >>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'}) 548 603 >>> headers = {"Content-type": "application/x-www-form-urlencoded", 549 604 ... "Accept": "text/plain"} 550 >>> conn = httplib.HTTPConnection(" musi-cal.mojam.com:80")551 >>> conn.request("POST", " /cgi-bin/query", params, headers)605 >>> conn = httplib.HTTPConnection("bugs.python.org") 606 >>> conn.request("POST", "", params, headers) 552 607 >>> response = conn.getresponse() 553 608 >>> print response.status, response.reason 554 200 OK609 302 Found 555 610 >>> data = response.read() 611 >>> data 612 'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>' 556 613 >>> conn.close() 557 614 615 Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The 616 difference lies only the server side where HTTP server will allow resources to 617 be created via ``PUT`` request. Here is an example session that shows how to do 618 ``PUT`` request using httplib:: 619 620 >>> # This creates an HTTP message 621 >>> # with the content of BODY as the enclosed representation 622 >>> # for the resource http://localhost:8080/foobar 623 ... 624 >>> import httplib 625 >>> BODY = "***filecontents***" 626 >>> conn = httplib.HTTPConnection("localhost", 8080) 627 >>> conn.request("PUT", "/file", BODY) 628 >>> response = conn.getresponse() 629 >>> print response.status, response.reason 630 200, OK 631
Note:
See TracChangeset
for help on using the changeset viewer.