http
The library is considered production ready.
from pykit import http
headers = {
'Host': '127.0.0.1',
'Accept-Language': 'en, mi',
}
try:
h = http.Client('127.0.0.1', 80)
# send http reqeust without body
# read response status line
# read response headers
h.request('/test.txt', method='GET', headers=headers)
status = h.status
# response code return from http server, type is int
# 200
# 302
# 404
# ...
res_headers = h.headers
# response headers except status line
# res_headers = {
# 'Content-Type': 'text/html;charset=utf-8',
# 'Content-Length': 1024,
# ...
# }
# get response body
print(h.read_body(None))
except (socket.error, http.HttpError) as e:
print(repr(e))
from pykit import http
import urllib
content = urllib.urlencode({'f': 'foo', 'b': 'bar'})
headers = {
'Host': 'www.example.com',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Content-Length': len(content),
}
try:
h = http.Client('127.0.0.1', 80)
# send http reqeust
h.send_request('http://www.example.com', method='POST', headers=headers)
# send http request body
h.send_body(content)
# read response status line and headers
status, headers = h.read_response()
# read response body
print(h.read_body(None))
except (socket.error, http.HttpError) as e:
print(repr(e))
HTTP/1.1 client
We find that httplib
must work in blocking mode and it can not have a timeout
when recving response.
Use this module, we can set timeout, if timeout raise a socket.timeout
.
syntax:
http.HttpError
The base class of the other exceptions in this module.
It is a subclass of Exception
.
syntax:
http.LineTooLongError
A subclass of HttpError
.
Raise if length of line is greater or equal than 65536
when read response status line, headers and get length of chunked block.
syntax:
http.ChunkedSizeError
A subclass of HttpError
.
Raise if failed to get length of chunked block.
syntax:
http.NotConnectedError
A subclass of HttpError
.
Raise if send data without connecting to server.
syntax:
http.ResponseNotReadyError
A subclass of HttpError
.
Raise if response is unavailable.
syntax:
http.HeadersError
A subclass of HttpError
.
Raise if failed to read response headers.
syntax:
http.BadStatusLineError
A subclass of HttpError
.
Raise if failed to read response status line.
syntax:
http.Client.status
Status code returned by server.
syntax:
http.Client.has_read
The length of response body that has been read.
syntax:
http.Client.headers
A dict
(header name, header value) of response headers.
syntax:
http.Client.content_length
Length of http response body, if body is chunked encoding,
it is None
.
syntax:
http.Client.chunked
Whether response body is chunked encoding or not.
syntax:
http.Client(host, port, timeout=60, stopwatch_kwargs=None, https_context=None)
HTTP client class
arguments:
-
host
: server address, ip or domain, type isstr
-
port
: server port, type isint
-
timeout
: set a timeout on blocking socket operations, unit is second, default is 60. The value argument can be a nonnegative float expressing seconds, orNone
. ifNone
, it is equivalent tosocket.setblocking(1)
. if0.0
, it is equivalent tosocket.setblocking(0)
. -
stopwatch_kwargs
: is an dictionary used as keyword arguments when initializing stopwatch instance.class StopWatch(object): def __init__(self, strict_assert=True, export_tracing_func=None, export_aggregated_timers_func=None, max_tracing_spans_for_path=1000, min_tracing_milliseconds=3, time_func=None):
-
https_context
: assl.SSLContext
instance describing the various SSL options. Defaults toNone
.
syntax:
http.Client.send_request(uri, method='GET', headers={})
Connect to server and send http request.
arguments:
-
uri
: specifies the object being requested -
method
: specifies an HTTP request method -
headers
: adict
(header name, header value) of http request headers
return: nothing
syntax:
http.Client.read_status()
Read response status line and return the status code.
Cache the response status code with http.Client.status
arguments: nothing
return: response status code.
syntax:
http.Client.read_headers()
Read and return the response headers.
Cache the response headers with http.Client.headers
arguments: nothing
return:
response headers, it is a dict
(header name , header value).
syntax:
http.Client.read_response()
Read response status line, headers and return them.
Cache the status code with http.Client.status
Cache the response headers with http.Client.headers
arguments: nothing
return: two values,
-
status: response status code, type is
int
-
headers: response headers, it is a
dict
(header name , header value).
syntax:
http.Client.request(uri, method='GET', headers={})
Send http request without body and read response status line, headers.
After it, get response status code with http.Client.status
,
get response headers with http.Client.headers
.
arguments:
-
uri
: specifies the object being requested -
method
: specifies an HTTP request method -
headers
: adict
(header name, header value) of http request headers
return: nothing
syntax:
http.Client.send_body(body)
Send http request body. It should be a str
of data
to send after the headers are finished
arguments:
body
: astr
of http request body.
return: nothing
syntax:
http.Client.read_body(size)
Read and return the response body.
arguments:
size
: need read size, if greater than left size use the min one, ifNone
read left unread body.
return: the response body.
syntax:
http.Client.readlines(delimiter)
Read and yield one line in response body each time.
arguments:
delimiter
: specify the delimiter between each line, defalut supporting\n
.
return: a generator yileding one line including delimiter in response body each time.
syntax:
http.Client.get_trace_str()
return: a string shows time spent on each phase of a request. The following fields would presents in it:
conn: 0.000001 -; send_header: 0.000000 -; recv_status: 0.000000 -; recv_header: 0.000000 -; recv_body: 0.000000 -; recv_body: 0.000000 -; exception: 0.000000 -; pykit.http.Client: 0.000002 Exception:ValueError
The numbers are time in second.
There might be less fields in the result, if request failed.
It is also possible some of the above fields appear more than once.
For example if a server responds a 100-Continue
status line and a 200-OK
status line, there would be two recv_status
fields.
If the caller calls http.Client.read_body
more than one time, there would be
more than one recv_body
fields.
syntax:
http.headers_add_host(headers, address)
If there is no Host field in the headers, insert the address as a Host into the headers.
arguments:
-
headers
: adict
(header name, header value) of http request headers -
address
: astring
represents a domain name
return: headers after adding
syntax:
http.request_add_host(request, address)
If the request has no headers field, or request['headers'] does not have a Host field, then add address to Host.
arguments:
-
request
: adict
(request key, request name) of http request -
address
: astring
represents a domain name
return: request after adding
Baohai Liu(刘保海) [email protected]
The MIT License (MIT)
Copyright (c) 2017 Baohai Liu(刘保海) [email protected]