Mageia Bugzilla – Attachment 12259 Details for
Bug 27407
python-urllib3 new security issue CVE-2020-26137
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
New Account
|
Forgot Password
Tests urllib3 module with a few simple examples
tutorial.py (text/plain), 2.50 KB, created by
Len Lawrence
on 2021-01-25 17:37:40 CET
(
hide
)
Description:
Tests urllib3 module with a few simple examples
Filename:
MIME Type:
Creator:
Len Lawrence
Created:
2021-01-25 17:37:40 CET
Size:
2.50 KB
patch
obsolete
># This python snippet exercizes the basic functions of urllib3. ># It is a truncated version of the introduction at https://zetcode.com/python/urllib3/ ># *** Install python3-certifi before running this *** ># This script was intended for python3 but seems to work with python2. > >import urllib3 >print( urllib3.__version__ ) ># Create pool manager to handle the details >http = urllib3.PoolManager( ) >url = 'http://webcode.me' ># GET something from the Web >resp = http.request( 'GET', url ) >print( resp.status ) >print( resp.data.decode('utf-8') ) ># HEAD request obtains just the header without the data >resp = http.request( 'HEAD', url ) >print( resp.headers['Server'] ) >print( resp.headers['Date'] ) >print( resp.headers['Content-Type'] ) >print( resp.headers['Last-Modified'] ) > ># Install python3-certifi to make this work > >import certifi ># Pass local root certificates to pool manager >url = 'https://httpbin.org/anything' >http = urllib3.PoolManager( ca_certs=certifi.where( ) ) >resp = http.request( 'GET', url ) >print( resp.status ) ># Use search parameters to return data as a JSON string >payload = { 'name': 'Peter', 'age': 23 } >url = 'https://httpbin.org/get' >req = http.request( 'GET', url, fields=payload ) >print( req.data.decode( 'utf-8' ) ) > >url = 'https://httpbin.org/post' >req = http.request( 'POST', url, fields={ 'name': 'John Doe' } ) >print( req.data.decode( 'utf-8' ) ) > >import json ># POST request in JSON format >payload = {'name': 'John Doe'} >encoded_data = json.dumps(payload).encode('utf-8') >resp = http.request( > 'POST', > 'https://httpbin.org/post', > body=encoded_data, > headers={ 'Content-Type': 'application/json' } ) >data = json.loads( resp.data.decode( 'utf-8' ) )['json'] >print( data ) ># GET binary data, using a 'with' clause to save the data in a specific file. ># $ file favicon.ico ># favicon.ico: MS Windows icon resource - 1 icon, 16x16 >url = 'http://webcode.me/favicon.ico' >req = http.request( 'GET', url ) >with open( 'favicon.ico', 'wb' ) as f: > f.write( req.data ) ># Stream data to local file >url = "https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf" >local_filename = url.split('/')[-1] >http = urllib3.PoolManager( ca_certs=certifi.where() ) >resp = http.request( > 'GET', > url, > preload_content=False ) >with open( local_filename, 'wb' ) as f: > for chunk in resp.stream( 1024 ): > f.write( chunk ) >resp.release_conn( ) > >url = 'https://httpbin.org/redirect-to?url=/' >resp = http.request( 'GET', url, redirect=True ) >print( resp.status ) >print( resp.geturl( ) ) >print( resp.info( ) ) > > > > > > > >
# This python snippet exercizes the basic functions of urllib3. # It is a truncated version of the introduction at https://zetcode.com/python/urllib3/ # *** Install python3-certifi before running this *** # This script was intended for python3 but seems to work with python2. import urllib3 print( urllib3.__version__ ) # Create pool manager to handle the details http = urllib3.PoolManager( ) url = 'http://webcode.me' # GET something from the Web resp = http.request( 'GET', url ) print( resp.status ) print( resp.data.decode('utf-8') ) # HEAD request obtains just the header without the data resp = http.request( 'HEAD', url ) print( resp.headers['Server'] ) print( resp.headers['Date'] ) print( resp.headers['Content-Type'] ) print( resp.headers['Last-Modified'] ) # Install python3-certifi to make this work import certifi # Pass local root certificates to pool manager url = 'https://httpbin.org/anything' http = urllib3.PoolManager( ca_certs=certifi.where( ) ) resp = http.request( 'GET', url ) print( resp.status ) # Use search parameters to return data as a JSON string payload = { 'name': 'Peter', 'age': 23 } url = 'https://httpbin.org/get' req = http.request( 'GET', url, fields=payload ) print( req.data.decode( 'utf-8' ) ) url = 'https://httpbin.org/post' req = http.request( 'POST', url, fields={ 'name': 'John Doe' } ) print( req.data.decode( 'utf-8' ) ) import json # POST request in JSON format payload = {'name': 'John Doe'} encoded_data = json.dumps(payload).encode('utf-8') resp = http.request( 'POST', 'https://httpbin.org/post', body=encoded_data, headers={ 'Content-Type': 'application/json' } ) data = json.loads( resp.data.decode( 'utf-8' ) )['json'] print( data ) # GET binary data, using a 'with' clause to save the data in a specific file. # $ file favicon.ico # favicon.ico: MS Windows icon resource - 1 icon, 16x16 url = 'http://webcode.me/favicon.ico' req = http.request( 'GET', url ) with open( 'favicon.ico', 'wb' ) as f: f.write( req.data ) # Stream data to local file url = "https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf" local_filename = url.split('/')[-1] http = urllib3.PoolManager( ca_certs=certifi.where() ) resp = http.request( 'GET', url, preload_content=False ) with open( local_filename, 'wb' ) as f: for chunk in resp.stream( 1024 ): f.write( chunk ) resp.release_conn( ) url = 'https://httpbin.org/redirect-to?url=/' resp = http.request( 'GET', url, redirect=True ) print( resp.status ) print( resp.geturl( ) ) print( resp.info( ) )
View Attachment As Raw
Actions:
View
Attachments on
bug 27407
: 12259 |
12260