slowdown.http
– HTTP/1.1 protocol support¶
This module defines a class, File
, which enhances
gevent.socket.socket by supporting buffering file interfaces and the
HTTP/1.1 protocol.
When the underlying handler of gevent.server.Server accepts
parameters socket and address, this module provides another class,
Handler, which turn the function that accepts the parameter rw
(which is a File
object) into the standard handler of
gevent.server.Server .
In general, the File
object is sometimes called rw , which
means the Read-Write Pair .
Examples:
# Working with gevent.server.Server
import gevent.server
import slowdown.http
def handler(rw):
# HTTP headers are stored in a dict named `environ`
print (rw.environ['REMOTE_ADDR'])
print (rw.environ['REMOTE_PORT'])
print (rw.environ['PATH_INFO'])
# Read the HTTP content. Bytes are returned.
data = rw.read()
# Send response to client
rw.start_response(
status='200 OK', # the default is '200 OK'
# the default is []
headers=[('Content-Type', 'text/plain')]
)
rw.write(b'It works!')
rw.close()
server = \
gevent.server.Server(
('0.0.0.0', 8080),
slowdown.http.Handler(handler)
)
server.serve_forever()
# Using cookies
import http.cookies
def handler(rw):
# Get cookies
# `None` will be returned if there are no cookies exists.
cookie = rw.cookie # `http.cookies.SimpleCookie` object
# Set cookies
new_cookie = http.cookies.SimpleCookie()
new_cookie['key'] = 'value'
rw.send_html_and_close(
content='<html>OK</html>',
cookie=new_cookie
)
Fast response utils:
Response Status |
Method |
---|---|
/ |
|
/ |
|
304 |
|
400 |
|
403 |
|
404 |
|
405 |
|
413 |
|
414 |
|
500 |
|
300 |
|
301 |
|
302 |
|
303 |
|
307 |
Example:
def handler(rw):
return rw.not_found()
-
class
slowdown.http.
File
(socket: gevent.socket.socket, reader: io.BufferedReader, environ: dict)¶ -
socket
¶ The original socket object
-
reader
¶ The reading stream
-
environ
¶ The HTTP headers
Accessing cookies as a http.cookies.SimpleCookie object
- Return type
http.cookies.SimpleCookie
-
start_response
(status:str='200 OK', headers:List[Tuple[str, str]])=None, cookie:http.cookies.SimpleCookie=None) → None¶ Send the http header.
-
start_chunked
(status:str='200 OK', headers:List[Tuple[str, str]])=None, cookie:http.cookies.SimpleCookie=None) → None¶ Set the transfer encoding to ‘chunked’ and send the http header.
-
send_response_and_close
(status:str='200 OK', headers:List[Tuple[str, str]])=None, content:Union[str, bytes]=None, cookie:http.cookies.SimpleCookie=None) → None¶
-
send_html_and_close
(status:str='200 OK', headers:List[Tuple[str, str]])=None, content:Union[str, bytes]=None, cookie:http.cookies.SimpleCookie=None, encoding:str=None) → None¶
-
read
(size: int = - 1) → bytes¶ Read the HTTP content at most size bytes, returned as a bytes object.
-
readline
(size: int = - 1) → bytes¶ Next line from the HTTP content, as a bytes object.
-
write
(data: bytes) → None¶ Send bytes to client.
-
sendall
(data: bytes) → None¶ Send bytes to client.
-
flush
()¶ Does nothing.
-
close
(disconnect: bool = False) → None¶ - Parameters
disconnect (bool) –
True close the connection
False complete the current request and keep alive
-
not_modified
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 304 Not Modified.
-
bad_request
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 400 Bad Request.
-
forbidden
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 403 Forbidden.
-
not_found
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 404 Not Found.
-
method_not_allowed
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 405 Method Not Allowed.
-
request_entity_too_large
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 413 Request Entity Too Large.
-
request_uri_too_large
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 414 Request-URI Too Large.
-
internal_server_error
(headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None) → None¶ 500 Internal Server Error.
-
multiple_choices
(urls: Union[str, List[str]], headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None, url_encoding: str = None) → None¶ 300 Multiple Choices.
-
moved_permanently
(urls: Union[str, List[str]], headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None, url_encoding: str = None) → None¶ 301 Moved Permanently.
-
found
(urls: Union[str, List[str]], headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None, url_encoding: str = None) → None¶ 302 Found.
-
see_other
(urls: Union[str, List[str]], headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None, url_encoding: str = None) → None¶ 300 See Other.
-
temporary_redirect
(urls: Union[str, List[str]], headers: List[Tuple[str, str]] = None, content: Union[str, bytes] = None, url_encoding: str = None) → None¶ 307 Temporary Redirect.
-