예제 #1
0
def safe_request(prog, method, *args, arg_encoding="UTF-8", out=stdout):
    parser = ArgumentParser(prog, usage="%(prog)s {:s} [options] uri [uri ...]".format(method))
    parser.add_argument("-1", "--single-receiver", action="store_true")
    parser.add_argument("-r", "--rx-buffer-size", metavar="SIZE", default=4194304)
    parser.add_argument("-v", "--verbose", action="store_true")
    parser.add_argument("-vv", "--very-verbose", action="store_true")
    parser.add_argument("uri", nargs="+")
    parsed = parser.parse_args(args)
    if parsed.verbose:
        watch("shortwave.http", level=INFO)
    if parsed.very_verbose:
        watch("shortwave.transmission", level=DEBUG)
    if parsed.single_receiver:
        receiver = HTTP.Rx()
        receiver.start()
    else:
        receiver = None

    connections = []
    http = None
    try:
        for uri in parsed.uri:
            scheme, authority, path, query, fragment = parse_uri(uri.encode(arg_encoding))
            if scheme and scheme != b"http":
                raise ValueError("Non-HTTP URI: %r" % uri)
            if authority:
                http = HTTP(authority, receiver, rx_buffer_size=parsed.rx_buffer_size)
                connections.append(http)
            target = build_uri(path=path, query=query, fragment=fragment)
            http.append(getattr(HTTPRequest, method)(target), ResponseWriter(out))
    finally:
        for http in connections:
            http.close()
        if receiver:
            receiver.stop()
예제 #2
0
def delete(prog, method, *args, arg_encoding="UTF-8", out=stdout):
    parser = ArgumentParser(prog, usage="%(prog)s {:s} [options] uri".format(method))
    parser.add_argument("-r", "--rx-buffer-size", metavar="SIZE", default=4194304)
    parser.add_argument("-v", "--verbose", action="store_true")
    parser.add_argument("-vv", "--very-verbose", action="store_true")
    parser.add_argument("uri")
    parsed = parser.parse_args(args)
    if parsed.verbose:
        watch("shortwave.http", level=INFO)
    if parsed.very_verbose:
        watch("shortwave.transmission", level=DEBUG)

    scheme, authority, path, query, fragment = parse_uri(parsed.uri.encode(arg_encoding))
    http = HTTP(authority, rx_buffer_size=parsed.rx_buffer_size, connection="close")
    target = build_uri(path=path, query=query, fragment=fragment)
    headers = {}
    try:
        http.append(HTTPRequest.delete(target, **headers), ResponseWriter(out))
    finally:
        http.close()
예제 #3
0
 def test_can_build_uri_from_scheme_port_and_path(self):
     built = build_uri(scheme=b"http", port=3456, path=b"/foo")
     assert built == b"http://:3456/foo"
예제 #4
0
 def test_can_build_uri_from_scheme_authority_and_port(self):
     built = build_uri(scheme=b"http", authority=b"*****@*****.**", port=3456)
     assert built == b"http://[email protected]:3456"
예제 #5
0
 def test_can_build_uri_from_scheme_authority_and_host(self):
     built = build_uri(scheme=b"http", authority=b"*****@*****.**", host=b"example.com")
     assert built == b"http://[email protected]"
예제 #6
0
 def test_can_build_uri_from_scheme_user_info_and_path(self):
     built = build_uri(scheme=b"http", user_info=b"bob", path=b"/foo")
     assert built == b"http://bob@/foo"
예제 #7
0
 def test_can_build_uri_from_scheme_user_info_and_host_port(self):
     built = build_uri(scheme=b"http", user_info=b"bob", host_port=b"example.com:3456")
     assert built == b"http://[email protected]:3456"
예제 #8
0
 def test_can_build_uri_from_scheme_and_host_port(self):
     built = build_uri(scheme=b"http", host_port=b"example.com:3456")
     assert built == b"http://example.com:3456"
예제 #9
0
 def test_can_build_uri_from_string(self):
     built = build_uri(uri=b"foo://example.com/")
     assert built == b"foo://example.com/"
예제 #10
0
 def test_can_build_uri_from_authority_and_absolute_path_reference(self):
     built = build_uri(authority=b"[email protected]:9999",
                       absolute_path_reference=b"/foo/bar?spam=eggs#mustard")
     assert built == b"//[email protected]:9999/foo/bar?spam=eggs#mustard"
예제 #11
0
 def test_can_build_uri_from_absolute_path_reference(self):
     built = build_uri(absolute_path_reference=b"/foo/bar?spam=eggs#mustard")
     assert built == b"/foo/bar?spam=eggs#mustard"
예제 #12
0
 def test_can_build_uri_from_scheme_hierarchical_part_query_and_fragment(self):
     built = build_uri(scheme=b"foo", hierarchical_part=b"//example.com/", query=b"spam=eggs",
                       fragment=b"mustard")
     assert built == b"foo://example.com/?spam=eggs#mustard"
예제 #13
0
 def test_can_build_uri_from_scheme_hierarchical_part_and_query(self):
     built = build_uri(scheme=b"foo", hierarchical_part=b"//example.com/", query=b"spam=eggs")
     assert built == b"foo://example.com/?spam=eggs"
예제 #14
0
 def test_can_build_uri_from_scheme_and_hierarchical_part(self):
     built = build_uri(scheme=b"foo", hierarchical_part=b"//example.com/")
     assert built == b"foo://example.com/"
예제 #15
0
 def test_can_build_uri_from_hierarchical_part(self):
     built = build_uri(hierarchical_part=b"//example.com/")
     assert built == b"//example.com/"
예제 #16
0
 def test_can_build_uri_from_scheme_host_and_path(self):
     built = build_uri(scheme=b"http", host=b"example.com", path=b"/foo/bar")
     assert built == b"http://example.com/foo/bar"
예제 #17
0
 def test_can_build_empty_uri(self):
     built = build_uri()
     assert built is b""