本文整理汇总了Python中coapthon.messages.request.Request类的典型用法代码示例。如果您正苦于以下问题:Python Request类的具体用法?Python Request怎么用?Python Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Request类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: discover
def discover(self, *args, **kwargs):
"""
:param args: request object
:param kwargs: dictionary with parameters
"""
if len(args) > 0:
request = args[0]
assert(isinstance(request, Request))
endpoint = request.destination
ip, port = endpoint
else:
request = Request()
path = kwargs['path']
assert(isinstance(path, str))
ip, port, path = self.parse_path(path)
request.destination = (ip, port)
if path == "":
path = defines.DISCOVERY_URL
request.uri_path = path
endpoint = (ip, port)
request.code = defines.inv_codes["GET"]
self.send(request, endpoint)
self.condition.acquire()
self.condition.wait()
message = self._response
key = hash(str(ip) + str(port) + str(message.mid))
if message.type == defines.inv_types["ACK"] and message.code == defines.inv_codes["EMPTY"] \
and key in self.sent.keys():
# Separate Response
self.send(request, endpoint)
self.condition.acquire()
self.condition.wait()
message = self._response
return message
示例2: post
def post(self, client_callback, *args, **kwargs):
if isinstance(args, tuple):
path, payload = args
req = Request()
req.uri_path = path
req._payload = payload
if "Token" in kwargs.keys():
req.token = kwargs.get("Token")
del kwargs["Token"]
if "MID" in kwargs.keys():
req.mid = kwargs.get("MID")
del kwargs["MID"]
if "Server" in kwargs.keys():
req.destination = kwargs.get("Server")
del kwargs["Server"]
else:
req = args[0]
for key in kwargs:
try:
o = Option()
o.number = defines.inv_options[key]
o.value = kwargs[key]
req.add_option(o)
except KeyError:
pass
req.code = defines.inv_codes['POST']
req.type = defines.inv_types["CON"]
self.send_callback(req, self.post_results, client_callback)
示例3: discover
def discover(self, client_callback, *args, **kwargs):
req = Request()
if "Token" in kwargs.keys():
req.token = kwargs.get("Token")
req.code = defines.inv_codes['GET']
req.uri_path = ".well-known/core"
req.type = defines.inv_types["CON"]
self.send_callback(req, self.discover_results, client_callback)
示例4: map
def map(self, request):
path = request.uri_path
if request.uri_path == defines.DISCOVERY_URL:
response = Response()
response.destination = request.source
response = self._resource_layer.discover(request, response)
self.result_forward(response, request)
server = self.root.find_complete(path)
if server is not None:
new_request = Request()
segments = server.find_path().split("/")
path = segments[2:]
path = "/".join(path)
segments = segments[1].split(":")
host = segments[0]
port = int(segments[1])
# new_request.destination = (host, port)
new_request.source = request.source
new_request.type = request.type
new_request._mid = (self._currentMID + 1) % (1 << 16)
new_request.code = request.code
new_request.proxy_uri = "coap://" + str(host) + ":" + str(port) + "/" + path
new_request.payload = request.payload
for option in request.options:
if option.name == defines.inv_options["Uri-Path"]:
continue
if option.name == defines.inv_options["Uri-Query"]:
continue
if option.name == defines.inv_options["Uri-Host"]:
continue
new_request.add_option(option)
return new_request
return None
示例5: test_td_coap_core_07
def test_td_coap_core_07(self):
print "TD_COAP_CORE_07"
path = "/test"
req = Request()
req.code = defines.inv_codes['PUT']
req.uri_path = path
req.type = defines.inv_types["NON"]
o = Option()
o.number = defines.inv_options["Content-Type"]
o.value = defines.inv_content_types["application/xml"]
req.add_option(o)
req._mid = self.current_mid
req.destination = self.server_address
req.payload = "<value>test</value>"
expected = Response()
expected.type = defines.inv_types["NON"]
expected._mid = None
expected.code = defines.responses["CHANGED"]
expected.token = None
expected.payload = None
self.current_mid += 1
self._test_plugtest(req, expected)
示例6: test_etag_deserialize
def test_etag_deserialize(self):
req = Request()
req.type = defines.Types["CON"]
req._mid = 1
req.etag = bytearray([0xC5])
serializer = Serializer()
serialized = serializer.serialize(req)
received_message = serializer.deserialize(serialized, ("127.0.0.1", 5683))
self.assertEqual(req.etag, received_message.etag)
示例7: delete
def delete(self, path, callback=None): # pragma: no cover
request = Request()
request.destination = self.server
request.code = defines.Codes.DELETE.number
request.uri_path = path
if callback is not None:
thread = threading.Thread(target=self._thread_body, args=(request, callback))
thread.start()
else:
self.protocol.send_message(request)
response = self.queue.get(block=True)
return response
示例8: discover
def discover(self, callback=None):
request = Request()
request.destination = self.server
request.code = defines.Codes.GET.number
request.uri_path = defines.DISCOVERY_URL
if callback is not None:
thread = threading.Thread(target=self._thread_body, args=(request, callback))
thread.start()
else:
self.protocol.send_message(request)
response = self.queue.get(block=True)
return response
示例9: get
def get(self, *args, **kwargs):
"""
:param args: request object
:param kwargs: dictionary with parameters
"""
if len(args) > 0:
request = args[0]
assert isinstance(request, Request)
endpoint = request.destination
ip, port = endpoint
else:
request = Request()
path = kwargs["path"]
assert isinstance(path, str)
ip, port, path = self.parse_path(path)
request.destination = (ip, port)
request.uri_path = path
endpoint = (ip, port)
request.code = defines.inv_codes["GET"]
self.send(request, endpoint)
future_time = random.uniform(defines.ACK_TIMEOUT, (defines.ACK_TIMEOUT * defines.ACK_RANDOM_FACTOR))
retransmit_count = 0
self.condition.acquire()
while True:
self.condition.wait(timeout=future_time)
if self._response is not None:
break
if request.type == defines.inv_types["CON"]:
if retransmit_count < defines.MAX_RETRANSMIT and (not request.acknowledged and not request.rejected):
print ("retransmit")
retransmit_count += 1
future_time *= 2
self.send(request, endpoint)
else:
print ("Give up on message: " + str(request.mid))
self.stop = True
break
message = self._response
self._response = None
key = hash(str(ip) + str(port) + str(message.mid))
if (
message.type == defines.inv_types["ACK"]
and message.code == defines.inv_codes["EMPTY"]
and key in self.sent.keys()
):
# Separate Response
self.condition.acquire()
self.condition.wait()
message = self._response
self._response = None
return message
示例10: mk_request
def mk_request(self, method, path):
"""
Create a request.
:param method: the CoAP method
:param path: the path of the request
:return: the request
"""
request = Request()
request.destination = self.server
request.code = method.number
request.uri_path = path
return request
示例11: put
def put(self, path, payload, callback=None):
request = Request()
request.destination = self.server
request.code = defines.Codes.PUT.number
request.uri_path = path
request.payload = payload
if callback is not None:
thread = threading.Thread(target=self._thread_body, args=(request, callback))
thread.start()
else:
self.protocol.send_message(request)
response = self.queue.get(block=True)
return response
示例12: test_get_not_found
def test_get_not_found(self):
print "\nGET /not_found\n"
args = ("/not_found",)
kwargs = {}
path = args[0]
req = Request()
for key in kwargs:
o = Option()
o.number = defines.inv_options[key]
o.value = kwargs[key]
req.add_option(o)
req.code = defines.inv_codes['GET']
req.uri_path = path
req.type = defines.inv_types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
expected = Response()
expected.type = defines.inv_types["ACK"]
expected._mid = self.current_mid
expected.code = defines.responses["NOT_FOUND"]
expected.token = None
expected.payload = None
self.current_mid += 1
self._test(req, expected)
示例13: test_long
def test_long(self):
print "\nGET /long\n"
args = ("/long",)
kwargs = {}
path = args[0]
req = Request()
for key in kwargs:
o = Option()
o.number = defines.inv_options[key]
o.value = kwargs[key]
req.add_option(o)
req.code = defines.inv_codes['GET']
req.uri_path = path
req.type = defines.inv_types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
expected = Response()
expected.type = defines.inv_types["ACK"]
expected._mid = self.current_mid
expected.code = None
expected.token = None
expected.payload = None
expected2 = Response()
expected2.type = defines.inv_types["CON"]
expected2.code = defines.responses["CONTENT"]
expected2.token = None
expected2.payload = "Long Time"
self.current_mid += 1
self._test_modular([(req, expected), (None, expected2)])
示例14: test_get_separate
def test_get_separate(self):
print "\nGET /separate\n"
args = ("/separate",)
kwargs = {}
path = args[0]
req = Request()
for key in kwargs:
o = Option()
o.number = defines.inv_options[key]
o.value = kwargs[key]
req.add_option(o)
req.code = defines.inv_codes['GET']
req.uri_path = path
req.type = defines.inv_types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
expected = Response()
expected.type = defines.inv_types["CON"]
expected.code = defines.responses["CONTENT"]
expected.token = None
expected.payload = "Separate"
self.current_mid += 1
self._test_separate(req, expected)
示例15: test_wrong_ep
def test_wrong_ep(self):
print("Endpoint name already exists")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = defines.Content_types["application/link-format"]
req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \
'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"'
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.SERVICE_UNAVAILABLE.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])