Python urllib.request.get_method() Examples

The following are 30 code examples of urllib.request.get_method(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib.request , or try the search function .
Example #1
Source File: seamicro.py    From maas with GNU Affero General Public License v3.0 7 votes vote down vote up
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"] 
Example #2
Source File: uploader.py    From VUT-FIT-IFJ-2017-toolkit with GNU General Public License v3.0 6 votes vote down vote up
def check_connection(self):
        request = urllib.request.Request(self._api_hostname)
        request.get_method = lambda: 'GET'

        try:
            data = self._request('/api/v1/service-online', {}, force=True)
        except URLError as e:
            TestLogger.log_warning('Problem with connecting to {} ({}).'.format(self._api_hostname, e))
            return
        else:
            self._has_connection = (
                200 >= self._last_response.status < 400
            ) and data.get('success')
            if data.get('msg'):
                TestLogger.log_warning(data.get('msg'))
            self.version = data.get('version') 
Example #3
Source File: test_urllib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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') 
Example #4
Source File: test_urllib.py    From android_universal with MIT License 5 votes vote down vote up
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') 
Example #5
Source File: test_urllib.py    From android_universal with MIT License 5 votes vote down vote up
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') 
Example #6
Source File: basic.py    From EasY_HaCk with Apache License 2.0 5 votes vote down vote up
def check_url(url,ua):
    global total_requests
    total_requests += 1
    request = urllib.request.Request(url)
    request.add_header('User-Agent', ua)
    request.get_method = lambda: 'HEAD'
    try:
        urllib.request.urlopen(request)
        return '1'
    except urllib.request.HTTPError:
        return '0' 
Example #7
Source File: http.py    From CloudBot with GNU General Public License v3.0 5 votes vote down vote up
def open(url, query_params=None, user_agent=None, post_data=None,
         referer=None, get_method=None, cookies=False, timeout=None, headers=None, **kwargs):
    if query_params is None:
        query_params = {}

    if user_agent is None:
        user_agent = ua_cloudbot

    query_params.update(kwargs)

    url = prepare_url(url, query_params)

    request = urllib.request.Request(url, post_data)

    if get_method is not None:
        request.get_method = lambda: get_method

    if headers is not None:
        for header_key, header_value in headers.items():
            request.add_header(header_key, header_value)

    request.add_header('User-Agent', user_agent)

    if referer is not None:
        request.add_header('Referer', referer)

    if cookies:
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
    else:
        opener = urllib.request.build_opener()

    if timeout:
        return opener.open(request, timeout=timeout)
    else:
        return opener.open(request) 
Example #8
Source File: link.py    From python-ngrok with GNU General Public License v3.0 5 votes vote down vote up
def api(endpoint, method="GET", data=None):
    if not PUBLIC_ID or not SECRET_TOKEN:
        raise Exception("Public ID and secret token must be declared")
    authstr = base64.encodestring('%s:%s' % (PUBLIC_ID, SECRET_TOKEN)).replace('\n', '')
    request = urllib.request.Request("https://api.ngrok.com/%s" % endpoint)
    if method != "GET":
        request.get_method = lambda: method
    request.add_header("Authorization", "Basic %s" % authstr)
    request.add_header("Content-Type", "application/json")
    response = urllib.request.urlopen(request, json.dumps(data) if data else None)
    try:
        return json.loads(response.read())
    except:
        return None 
Example #9
Source File: client.py    From python-ngrok with GNU General Public License v3.0 5 votes vote down vote up
def api(endpoint, method="GET", data=None, params=[]):
    base_url = BASE_URL or "http://127.0.0.1:4040/"
    if params:
        endpoint += "?%s" % urlencode([(x, params[x]) for x in params])
    request = urllib.request.Request("%sapi/%s" % (base_url, endpoint))
    if method != "GET":
        request.get_method = lambda: method
    request.add_header("Content-Type", "application/json")
    response = urllib.request.urlopen(request, json.dumps(data) if data else None)
    try:
        return json.loads(response.read())
    except:
        return None 
Example #10
Source File: utils_log.py    From motu-client-python with GNU Lesser General Public License v3.0 5 votes vote down vote up
def http_request(self, request):
        host, full_url = request.host, request.get_full_url()
        url_path = full_url[full_url.find(host) + len(host):]
        log_url ( self.log, "Requesting: ", request.get_full_url(), TRACE_LEVEL )
        self.log.log(self.log_level, "%s %s" % (request.get_method(), url_path))

        for header in request.header_items():
            self.log.log(self.log_level, " . %s: %s" % header[:])

        return request 
Example #11
Source File: test_urllib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #12
Source File: test_urllib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #13
Source File: arachni.py    From PassiveScanner with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #14
Source File: arachni.py    From PassiveScanner with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #15
Source File: download_gulp_mrw.py    From pvse with MIT License 5 votes vote down vote up
def url_is_alive(url):
  request = urllib.request.Request(url)
  request.get_method = lambda: 'HEAD'
  try:
    urllib.request.urlopen(request)
    return True
  except urllib.request.HTTPError:
    return False 
Example #16
Source File: basic.py    From CMSeeK with GNU General Public License v3.0 5 votes vote down vote up
def check_url(url,ua):
    global total_requests
    total_requests += 1
    request = urllib.request.Request(url)
    request.add_header('User-Agent', ua)
    request.get_method = lambda: 'HEAD'
    try:
        urllib.request.urlopen(request)
        return '1'
    except urllib.request.HTTPError:
        return '0' 
Example #17
Source File: nexpose.py    From nexpose-client-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #18
Source File: test_urllib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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') 
Example #19
Source File: ydl.py    From brozzler with Apache License 2.0 5 votes vote down vote up
def _http_response(self, request, response):
        fetch = {
            'url': request.full_url,
            'method': request.get_method(),
            'response_code': response.code,
            'response_headers': response.headers,
        }
        self.fetches.append(fetch)
        return response 
Example #20
Source File: request.py    From Imogen with MIT License 5 votes vote down vote up
def _get_content_length(self, request):
        return http.client.HTTPConnection._get_content_length(
            request.data,
            request.get_method()) 
Example #21
Source File: request.py    From Imogen with MIT License 5 votes vote down vote up
def redirect_request(self, req, fp, code, msg, headers, newurl):
        """Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods when a
        redirection response is received.  If a redirection should
        take place, return a new Request to allow http_error_30x to
        perform the redirect.  Otherwise, raise HTTPError if no-one
        else should try to handle this url.  Return None if you can't
        but another Handler might.
        """
        m = req.get_method()
        if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
            or code in (301, 302, 303) and m == "POST")):
            raise HTTPError(req.full_url, code, msg, headers, fp)

        # Strictly (according to RFC 2616), 301 or 302 in response to
        # a POST MUST NOT cause a redirection without confirmation
        # from the user (of urllib.request, in this case).  In practice,
        # essentially all clients do redirect in this case, so we do
        # the same.

        # Be conciliant with URIs containing a space.  This is mainly
        # redundant with the more complete encoding done in http_error_302(),
        # but it is kept for compatibility with other callers.
        newurl = newurl.replace(' ', '%20')

        CONTENT_HEADERS = ("content-length", "content-type")
        newheaders = {k: v for k, v in req.headers.items()
                      if k.lower() not in CONTENT_HEADERS}
        return Request(newurl,
                       headers=newheaders,
                       origin_req_host=req.origin_req_host,
                       unverifiable=True)

    # Implementation note: To avoid the server sending us into an
    # infinite loop, the request object needs to track what URLs we
    # have already seen.  Do this by adding a handler-specific
    # attribute to the Request object. 
Example #22
Source File: request.py    From Imogen with MIT License 5 votes vote down vote up
def get_method(self):
        """Return a string indicating the HTTP request method."""
        default_method = "POST" if self.data is not None else "GET"
        return getattr(self, 'method', default_method) 
Example #23
Source File: test_urllib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #24
Source File: test_urllib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #25
Source File: http.py    From CloudBot with GNU General Public License v3.0 5 votes vote down vote up
def open(url, query_params=None, user_agent=None, post_data=None,
         referer=None, get_method=None, cookies=False, timeout=None, headers=None,
         **kwargs):  # pylint: disable=locally-disabled, redefined-builtin  # pragma: no cover
    warnings.warn(
        "http.open() is deprecated, use http.open_request() instead.",
        DeprecationWarning
    )

    return open_request(
        url, query_params=query_params, user_agent=user_agent, post_data=post_data, referer=referer,
        get_method=get_method, cookies=cookies, timeout=timeout, headers=headers, **kwargs
    ) 
Example #26
Source File: http.py    From CloudBot with GNU General Public License v3.0 5 votes vote down vote up
def open_request(url, query_params=None, user_agent=None, post_data=None, referer=None, get_method=None, cookies=False,
                 timeout=None, headers=None, **kwargs):
    if query_params is None:
        query_params = {}

    if user_agent is None:
        user_agent = ua_cloudbot

    query_params.update(kwargs)

    url = prepare_url(url, query_params)

    request = urllib.request.Request(url, post_data)

    if get_method is not None:
        request.get_method = lambda: get_method

    if headers is not None:
        for header_key, header_value in headers.items():
            request.add_header(header_key, header_value)

    request.add_header('User-Agent', user_agent)

    if referer is not None:
        request.add_header('Referer', referer)

    if cookies:
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
    else:
        opener = urllib.request.build_opener()

    if timeout:
        return opener.open(request, timeout=timeout)

    return opener.open(request)


# noinspection PyShadowingBuiltins 
Example #27
Source File: shcheck.py    From shcheck with GNU General Public License v3.0 5 votes vote down vote up
def check_target(target, options):
    '''
    Just put a protocol to a valid IP and check if connection works,
    returning HEAD response
    '''
    # Recover used options
    ssldisabled = options.ssldisabled
    useget = options.useget
    proxy = options.proxy
    response = None

    target = normalize(target)

    try:
        request = urllib.request.Request(target, headers=client_headers)

        # Set method
        method = 'GET' if useget else 'HEAD'
        request.get_method = lambda: method

        # Set proxy
        set_proxy(proxy)
        # Set certificate validation 
        if ssldisabled:
            context = get_unsafe_context()
            response = urllib.request.urlopen(request, timeout=10, context=context)
        else:
            response = urllib.request.urlopen(request, timeout=10)

    except Exception as e:
        print_error(e)
        sys.exit(1)

    if response is not None:
        return response
    print("Couldn't read a response from server.")
    sys.exit(3) 
Example #28
Source File: nexpose.py    From nexpose-client-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #29
Source File: nexpose.py    From nexpose-client-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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') 
Example #30
Source File: bootstrap.py    From katello-client-bootstrap with GNU General Public License v2.0 4 votes vote down vote up
def call_api(url, data=None, method='GET'):
    """
    Helper function to place an API call returning JSON results and doing
    some error handling. Any error results in an exit.
    """
    try:
        request = urllib_request(url)
        if options.verbose:
            print('url: %s' % url)
            print('method: %s' % method)
            print('data: %s' % json.dumps(data, sort_keys=False, indent=2))
        auth_string = '%s:%s' % (options.login, options.password)
        base64string = base64.b64encode(auth_string.encode('utf-8')).decode().strip()
        request.add_header("Authorization", "Basic %s" % base64string)
        request.add_header("Content-Type", "application/json")
        request.add_header("Accept", "application/json")
        if data:
            if hasattr(request, 'add_data'):
                request.add_data(json.dumps(data))
            else:
                request.data = json.dumps(data).encode()
        request.get_method = lambda: method
        if sys.version_info >= (2, 6):
            result = urllib_urlopen(request, timeout=options.timeout)
        else:
            result = urllib_urlopen(request)
        jsonresult = json.load(result)
        if options.verbose:
            print('result: %s' % json.dumps(jsonresult, sort_keys=False, indent=2))
        return jsonresult
    except urllib_urlerror:
        exception = sys.exc_info()[1]
        print('An error occurred: %s' % exception)
        print('url: %s' % url)
        if isinstance(exception, urllib_httperror):
            print('code: %s' % exception.code)  # pylint:disable=no-member
        if data:
            print('data: %s' % json.dumps(data, sort_keys=False, indent=2))
        try:
            jsonerr = json.load(exception)
            print('error: %s' % json.dumps(jsonerr, sort_keys=False, indent=2))
        except:  # noqa: E722, pylint:disable=bare-except
            print('error: %s' % exception)
        sys.exit(1)
    except Exception:  # pylint:disable=broad-except
        exception = sys.exc_info()[1]
        print("FATAL Error - %s" % (exception))
        sys.exit(2)