我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用urllib.request.get_method()。
def put(self, location, params=None): """Dispatch a PUT request to a SeaMicro chassis. The seamicro box has order-dependent HTTP parameters, so we build our own get URL, and use a list vs. a dict for data, as the order is implicit. """ opener = urllib.request.build_opener(urllib.request.HTTPHandler) url = self.build_url(location, params) request = urllib.request.Request(url) request.get_method = lambda: 'PUT' request.add_header('content-type', 'text/json') response = opener.open(request) json_data = self.parse_response(url, response) return json_data['result']
def test_default_values(self): Request = urllib.request.Request request = Request("http://www.python.org") self.assertEqual(request.get_method(), 'GET') request = Request("http://www.python.org", {}) self.assertEqual(request.get_method(), 'POST')
def test_with_method_arg(self): Request = urllib.request.Request request = Request("http://www.python.org", method='HEAD') self.assertEqual(request.method, 'HEAD') self.assertEqual(request.get_method(), 'HEAD') request = Request("http://www.python.org", {}, method='HEAD') self.assertEqual(request.method, 'HEAD') self.assertEqual(request.get_method(), 'HEAD') request = Request("http://www.python.org", method='GET') self.assertEqual(request.get_method(), 'GET') request.method = 'HEAD' self.assertEqual(request.get_method(), 'HEAD')
def OpenWebRequest(uri, post_data, headers, timeout, get_method=None): request = urllib.request.Request(uri, post_data, headers) if get_method: request.get_method = get_method if timeout == 0: response = urllib.request.urlopen(request) else: response = urllib.request.urlopen(request, timeout=timeout) return response
def ExecuteWebRequest(uri, post_data, headers, timeout, get_method=None): response = OpenWebRequest(uri, post_data, headers, timeout, get_method) return response.read().decode('utf-8')
def ExecuteWithPostData_JSON(session_id, uri, sub_url, timeout, post_data, get_method): headers = CreateHeadersWithSessionCookieAndCustomHeader(session_id) headers["Content-Type"] = "application/json; charset=UTF-8" if isinstance(post_data, dict) or isinstance(post_data, list): post_data = json.dumps(post_data, separators=(',', ':')) post_data = post_data.encode('utf-8') return ExecuteWebRequest(uri + sub_url, post_data, headers, timeout, get_method)
def put_request(self, api_path): request = urllib.request.Request(self.arachni_url + api_path) request.get_method = lambda: 'PUT' return urllib.request.urlopen(request).read().decode('utf8')
def delete_request(self, api_path): request = urllib.request.Request(self.arachni_url + api_path) request.get_method = lambda: 'DELETE' return urllib.request.urlopen(request).read().decode('utf8')