aboutsummaryrefslogtreecommitdiffstats
path: root/netlib
diff options
context:
space:
mode:
Diffstat (limited to 'netlib')
-rw-r--r--netlib/http/http1/assemble.py4
-rw-r--r--netlib/http/request.py8
-rw-r--r--netlib/http/response.py2
-rw-r--r--netlib/http/url.py15
-rw-r--r--netlib/human.py14
-rw-r--r--netlib/tcp.py6
-rw-r--r--netlib/utils.py15
7 files changed, 39 insertions, 25 deletions
diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py
index 00d1563b..511328f1 100644
--- a/netlib/http/http1/assemble.py
+++ b/netlib/http/http1/assemble.py
@@ -1,6 +1,6 @@
from __future__ import absolute_import, print_function, division
-from netlib import utils
+import netlib.http.url
from netlib import exceptions
@@ -82,7 +82,7 @@ def _assemble_request_headers(request_data):
"""
headers = request_data.headers.copy()
if "host" not in headers and request_data.scheme and request_data.host and request_data.port:
- headers["host"] = utils.hostport(
+ headers["host"] = netlib.http.url.hostport(
request_data.scheme,
request_data.host,
request_data.port
diff --git a/netlib/http/request.py b/netlib/http/request.py
index 91d5f020..01801d42 100644
--- a/netlib/http/request.py
+++ b/netlib/http/request.py
@@ -227,7 +227,7 @@ class Request(message.Message):
def query(self):
# type: () -> multidict.MultiDictView
"""
- The request query string as an :py:class:`MultiDictView` object.
+ The request query string as an :py:class:`~netlib.multidict.MultiDictView` object.
"""
return multidict.MultiDictView(
self._get_query,
@@ -254,7 +254,7 @@ class Request(message.Message):
"""
The request cookies.
- An empty :py:class:`multidict.MultiDictView` object if the cookie monster ate them all.
+ An empty :py:class:`~netlib.multidict.MultiDictView` object if the cookie monster ate them all.
"""
return multidict.MultiDictView(
self._get_cookies,
@@ -329,7 +329,7 @@ class Request(message.Message):
@property
def urlencoded_form(self):
"""
- The URL-encoded form data as an :py:class:`multidict.MultiDictView` object.
+ The URL-encoded form data as an :py:class:`~netlib.multidict.MultiDictView` object.
An empty multidict.MultiDictView if the content-type indicates non-form data
or the content could not be parsed.
"""
@@ -359,7 +359,7 @@ class Request(message.Message):
@property
def multipart_form(self):
"""
- The multipart form data as an :py:class:`MultipartFormDict` object.
+ The multipart form data as an :py:class:`~netlib.multidict.MultiDictView` object.
None if the content-type indicates non-form data.
"""
return multidict.MultiDictView(
diff --git a/netlib/http/response.py b/netlib/http/response.py
index 44b58be6..7dabfcab 100644
--- a/netlib/http/response.py
+++ b/netlib/http/response.py
@@ -73,7 +73,7 @@ class Response(message.Message):
def cookies(self):
# type: () -> multidict.MultiDictView
"""
- The response cookies. A possibly empty :py:class:`multidict.MultiDictView`, where the keys are
+ The response cookies. A possibly empty :py:class:`~netlib.multidict.MultiDictView`, where the keys are
cookie name strings, and values are (value, attr) tuples. Value is a string, and attr is
an ODictCaseless containing cookie attributes. Within attrs, unary attributes (e.g. HTTPOnly)
are indicated by a Null value.
diff --git a/netlib/http/url.py b/netlib/http/url.py
index 5d461387..2fc6e7ee 100644
--- a/netlib/http/url.py
+++ b/netlib/http/url.py
@@ -78,7 +78,7 @@ def unparse(scheme, host, port, path=""):
"""
if path == "*":
path = ""
- return "%s://%s%s" % (scheme, utils.hostport(scheme, host, port), path)
+ return "%s://%s%s" % (scheme, hostport(scheme, host, port), path)
def encode(s):
@@ -94,3 +94,16 @@ def decode(s):
Takes a urlencoded string and returns a list of (key, value) tuples.
"""
return urllib.parse.parse_qsl(s, keep_blank_values=True)
+
+
+def hostport(scheme, host, port):
+ """
+ Returns the host component, with a port specifcation if needed.
+ """
+ if (port, scheme) in [(80, "http"), (443, "https"), (80, b"http"), (443, b"https")]:
+ return host
+ else:
+ if isinstance(host, six.binary_type):
+ return b"%s:%d" % (host, port)
+ else:
+ return "%s:%d" % (host, port)
diff --git a/netlib/human.py b/netlib/human.py
index a007adc7..72e96d30 100644
--- a/netlib/human.py
+++ b/netlib/human.py
@@ -1,3 +1,6 @@
+import datetime
+import time
+
SIZE_TABLE = [
("b", 1024 ** 0),
@@ -48,3 +51,14 @@ def pretty_duration(secs):
return formatter.format(secs)
# less than 1 sec
return "{:.0f}ms".format(secs * 1000)
+
+
+def format_timestamp(s):
+ s = time.localtime(s)
+ d = datetime.datetime.fromtimestamp(time.mktime(s))
+ return d.strftime("%Y-%m-%d %H:%M:%S")
+
+
+def format_timestamp_with_milli(s):
+ d = datetime.datetime.fromtimestamp(s)
+ return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
diff --git a/netlib/tcp.py b/netlib/tcp.py
index de12102e..0eec326b 100644
--- a/netlib/tcp.py
+++ b/netlib/tcp.py
@@ -580,8 +580,10 @@ class _Connection(object):
@contextlib.contextmanager
def _closer(client):
- yield
- client.close()
+ try:
+ yield
+ finally:
+ client.close()
class TCPClient(_Connection):
diff --git a/netlib/utils.py b/netlib/utils.py
index b4b99679..79340cbd 100644
--- a/netlib/utils.py
+++ b/netlib/utils.py
@@ -4,8 +4,6 @@ import re
import importlib
import inspect
-import six
-
def setbit(byte, offset, value):
"""
@@ -94,16 +92,3 @@ def is_valid_host(host):
def is_valid_port(port):
return 0 <= port <= 65535
-
-
-def hostport(scheme, host, port):
- """
- Returns the host component, with a port specifcation if needed.
- """
- if (port, scheme) in [(80, "http"), (443, "https"), (80, b"http"), (443, b"https")]:
- return host
- else:
- if isinstance(host, six.binary_type):
- return b"%s:%d" % (host, port)
- else:
- return "%s:%d" % (host, port)