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

Skip to content

Commit 1428e13

Browse files
committed
#17307 - merge from 3.3
2 parents 7701e6e + e66cc81 commit 1428e13

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
@@ -1141,6 +1141,15 @@ The code for the sample CGI used in the above example is::
11411141
data = sys.stdin.read()
11421142
print('Content-type: text-plain\n\nGot Data: "%s"' % data)
11431143

1144+
Here is an example of doing a ``PUT`` request using :class:`Request`::
1145+
1146+
import urllib.request
1147+
DATA=b'some data'
1148+
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
1149+
f = urllib.request.urlopen(req)
1150+
print(f.status)
1151+
print(f.reason)
1152+
11441153
Use of Basic HTTP Authentication::
11451154

11461155
import urllib.request

0 commit comments

Comments
 (0)