Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e66cc81

Browse files
committed
#17307 - merge from 3.2
2 parents 115309a + b5fe247 commit e66cc81

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

Doc/library/http.client.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,24 @@ Here is an example session that shows how to ``POST`` requests::
633633
b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
634634
>>> conn.close()
635635

636+
Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The
637+
difference lies only the server side where HTTP server will allow resources to
638+
be created via ``PUT`` request. It should be noted that custom HTTP methods
639+
+are also handled in :class:`urllib.request.Request` by sending the appropriate
640+
+method attribute.Here is an example session that shows how to do ``PUT``
641+
request using http.client::
642+
643+
>>> # This creates an HTTP message
644+
>>> # with the content of BODY as the enclosed representation
645+
>>> # for the resource http://localhost:8080/foobar
646+
...
647+
>>> import http.client
648+
>>> BODY = "***filecontents***"
649+
>>> conn = http.client.HTTPConnection("localhost", 8080)
650+
>>> conn.request("PUT", "/file", BODY)
651+
>>> response = conn.getresponse()
652+
>>> print(resp.status, response.reason)
653+
200, OK
636654

637655
.. _httpmessage-objects:
638656

Doc/library/urllib.request.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,15 @@ The code for the sample CGI used in the above example is::
11091109
data = sys.stdin.read()
11101110
print('Content-type: text-plain\n\nGot Data: "%s"' % data)
11111111

1112+
Here is an example of doing a ``PUT`` request using :class:`Request`::
1113+
1114+
import urllib.request
1115+
DATA=b'some data'
1116+
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
1117+
f = urllib.request.urlopen(req)
1118+
print(f.status)
1119+
print(f.reason)
1120+
11121121
Use of Basic HTTP Authentication::
11131122

11141123
import urllib.request

0 commit comments

Comments
 (0)