Skip to content

bpo-38193: Add http.client script #26775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,3 +1517,47 @@ def __init__(self, *pos, **kw):

# for backwards compatibility
error = HTTPException

def _get_http_version(response_version):
if response.version == 10:
return '1.0'
if response.version == 11:
return '1.1'
return 'unknown'


if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--method', '-m', default='GET',
help='HTTP method used in the request'
'[default:current directory]')
parser.add_argument('--output', '-o', default='',
nargs='?',
help='File to save the response body')
parser.add_argument('url', help='URL to make the request to')
args = parser.parse_args()

from urllib.parse import urlparse
parsed_url = urlparse(args.url)

http_connection = HTTPConnection(parsed_url.netloc)
http_connection.request(method=args.method, url=parsed_url.path)
try:
response = http_connection.getresponse()
response_body = response.read()
if args.output:
with open(args.output, 'wb+') as output_file:
output_file.write(response_body)
http_version = _get_http_version(response.version)
print('HTTP/%s %d %s' % (http_version, response.status, response.reason))
print(response.headers.as_string().strip())
print()
try:
print(response_body.decode('utf-8'))
except UnicodeDecodeError:
print(response_body.hex())
finally:
http_connection.close()

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enabled http.client module script to do simple HTTP requests
Loading