예제 #1
0
    def getHttpContent(self, url='', extraHeader={}, data=None, timeout=5):
        if data == None:
            request = urllib3.request(url=url)
        else:
            request = urllib3.request(url=url, data=data)

        for headerKey in extraHeader.keys:
            request.add_header(headerKey, extraHeader[headerKey])
예제 #2
0
 def test_top_level_request_with_body_and_json(self) -> None:
     match = "request got values for both 'body' and 'json' parameters which are mutually exclusive"
     with pytest.raises(TypeError, match=match):
         body = {"attribute": "value"}
         request(method="POST",
                 url=f"{self.base_url}/echo",
                 body="",
                 json=body)
예제 #3
0
    def test_top_level_request_with_invalid_body(self) -> None:
        class BadBody:
            def __repr__(self) -> str:
                return "<BadBody>"

        with pytest.raises(TypeError) as e:
            request(
                method="POST",
                url=f"{self.base_url}/echo",
                body=BadBody(),  # type: ignore[arg-type]
            )
        assert str(
            e.value) == ("'body' must be a bytes-like object, file-like "
                         "object, or iterable. Instead was <BadBody>")
예제 #4
0
    def test_top_level_request_with_decode_content(self) -> None:
        r = request(
            "GET",
            f"{self.base_url}/encodingrequest",
            headers={"accept-encoding": "gzip"},
            decode_content=False,
        )
        assert r.status == 200
        assert gzip.decompress(r.data) == b"hello, world!"

        r = request(
            "GET",
            f"{self.base_url}/encodingrequest",
            headers={"accept-encoding": "gzip"},
            decode_content=True,
        )
        assert r.status == 200
        assert r.data == b"hello, world!"
예제 #5
0
    def test_top_level_request_with_redirect(self) -> None:
        r = request(
            "GET",
            f"{self.base_url}/redirect",
            fields={"target": f"{self.base_url}/"},
            redirect=False,
        )

        assert r.status == 303

        r = request(
            "GET",
            f"{self.base_url}/redirect",
            fields={"target": f"{self.base_url}/"},
            redirect=True,
        )

        assert r.status == 200
        assert r.data == b"Dummy server!"
예제 #6
0
파일: util.py 프로젝트: BridgetX/pyqt5ggpo
def checkUpdate():
    versionurl = 'https://raw.github.com/BridgetX/pyqt5ggpo/master/VERSION'
    # noinspection PyBroadException
    http = urllib3.PoolManager()
    try:
        response = urllib3.request('GET', versionurl, timeout=2)
        #currently throwns an error as it doesn't just pull the '42' from the url and therefore can't be an int, can be fixed with BeautifulSoup but i'm looking for a method that doesn't require it.
        latestVersion = int(response.read().strip())
        return latestVersion - int(copyright.__version__)
    except:
        pass
예제 #7
0
    def do_request(self, method, params=None):
        """Make request to Zabbix API.

        :type method: str
        :param method: ZabbixAPI method, like: `apiinfo.version`.

        :type params: str
        :param params: ZabbixAPI method arguments.

        >>> from pyzabbix import ZabbixAPI
        >>> z = ZabbixAPI()
        >>> apiinfo = z.do_request('apiinfo.version')
        """

        request_json = {
            'jsonrpc': '2.0',
            'method': method,
            'params': params or {},
            'id': '1',
        }

        # apiinfo.version and user.login doesn't require auth token
        if self.auth and (method not in ('apiinfo.version', 'user.login')):
            request_json['auth'] = self.auth

        logger.debug('urllib2.request({0}, {1})'.format(
            self.url, json.dumps(request_json)))

        data = json.dumps(request_json)
        if not isinstance(data, bytes):
            data = data.encode("utf-8")

        req = urllib2.request(self.url, data)
        req.get_method = lambda: 'POST'
        req.add_header('Content-Type', 'application/json-rpc')

        try:
            res = urlopen(req)
            res_str = res.read().decode('utf-8')
            res_json = json.loads(res_str)
        except ValueError as e:
            raise ZabbixAPIException("Unable to parse json: %s" % e.message)

        res_str = json.dumps(res_json, indent=4, separators=(',', ': '))
        logger.debug("Response Body: %s", res_str)

        if 'error' in res_json:
            err = res_json['error'].copy()
            err.update({'json': str(request_json)})
            msg_str = "Error {code}: {message}, {data} while sending {json}"
            msg = msg_str.format(**err)
            raise ZabbixAPIException(msg, err['code'])

        return res_json
def measureWebTraffic(url):
    '''
    Web traffic is calculated for legitimate websites, the absence of it
    is a sign of phishing website
    '''
    try:
        rank = \
            bs4.BeautifulSoup(urllib3.request("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find(
                "REACH")['RANK']
    except TypeError:
        return -1
    rank = int(rank)
    return 1 if rank < 100000 else 0
예제 #9
0
 def test_request_with_json(self, headers: HTTPHeaderDict) -> None:
     body = {"attribute": "value"}
     r = request(
         method="POST", url=f"{self.base_url}/echo_json", headers=headers, json=body
     )
     assert r.status == 200
     assert r.json() == body
     if headers is not None and "application/json" not in headers.values():
         assert "text/plain" in r.headers["Content-Type"].replace(" ", "").split(",")
     else:
         assert "application/json" in r.headers["Content-Type"].replace(
             " ", ""
         ).split(",")
예제 #10
0
    def test_top_level_request_with_timeout(self) -> None:
        with mock.patch("urllib3.poolmanager.RequestMethods.request") as mockRequest:
            mockRequest.return_value = HTTPResponse(status=200)

            r = request("GET", f"{self.base_url}/redirect", timeout=2.5)

            assert r.status == 200

            mockRequest.assert_called_with(
                "GET",
                f"{self.base_url}/redirect",
                body=None,
                fields=None,
                headers=None,
                preload_content=True,
                decode_content=True,
                redirect=True,
                retries=None,
                timeout=2.5,
                json=None,
            )
예제 #11
0
def load_graph(graph_url):
    """
    Function that loads a graph given the URL
    for a text representation of the graph
    
    Returns a dictionary that models a graph
    """
    graph_file = urllib3.request(graph_url)
    graph_text = graph_file.read()
    graph_lines = graph_text.split('\n')
    graph_lines = graph_lines[:-1]

    print("Loaded graph with", len(graph_lines), "nodes")

    answer_graph = {}
    for line in graph_lines:
        neighbors = line.split(' ')
        node = int(neighbors[0])
        answer_graph[node] = set([])
        for neighbor in neighbors[1:-1]:
            answer_graph[node].add(int(neighbor))

    return answer_graph
예제 #12
0
파일: es_os.py 프로젝트: yaolinxia/practice
 def post(self, data):
     req = urllib3.request(
         self.url, data,
         {"Content-Type": "application/json; charset=UTF-8"})
     urllib3.connection(req)
예제 #13
0
 def test_top_level_request_with_body(self) -> None:
     r = request("POST", f"{self.base_url}/echo", body=b"test")
     assert r.status == 200
     assert r.data == b"test"
예제 #14
0
img_url = 'http://upload-images.jianshu.io/upload_images/7575721-40c847532432e852.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240'
# 获取token
get_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(
    app_key, secret_key)
token = requests.get(url=get_token_url).json().get(
    "access_token")  # 从获取token接口的响应中取得token值
print(token)
# 识别图片文字
orc_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}'.format(
    token)
data = {"url": img_url}
res = requests.post(url=orc_url, data=data)
print(json.dumps(res.json(), indent=2, ensure_ascii=False))  # 格式化输出
'''
人脸检测与属性分析
'''

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"

params = "{\"image\":\"027d8308a2ec665acb1bdf63e513bcb9\",\"image_type\":\"FACE_TOKEN\",\"face_field\":\"faceshape,facetype\"}"

access_token = '24.c08c7ea0f7e1ded70569cb5b809f43a1.2592000.1549701157.282335-15391346'
request_url = request_url + "?access_token=" + access_token
request = urllib3.request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
#response = urllib3.urlopen(request)
response = urllib3.connection_from_url(request)
content = response.read()
if content:
    print(content)
예제 #15
0
파일: firstbug.py 프로젝트: yhpyc/python3
import urllib3
response = urllib3.request("https://www.baidu.com")
print(response.read())
예제 #16
0
파일: demo.py 프로젝트: Easy-wei/vscode
import urllib3

response = urllib3.request('http://www.baidu.com')

print(response.read())
예제 #17
0
    def test_top_level_request_with_retries(self) -> None:
        r = request("GET", f"{self.base_url}/redirect", retries=False)
        assert r.status == 303

        r = request("GET", f"{self.base_url}/redirect", retries=3)
        assert r.status == 200
예제 #18
0
 def test_top_level_request(self) -> None:
     r = request("GET", f"{self.base_url}/")
     assert r.status == 200
     assert r.data == b"Dummy server!"
예제 #19
0
 def test_top_level_request_without_keyword_args(self) -> None:
     body = ""
     with pytest.raises(TypeError):
         request("GET", f"{self.base_url}/", body)  # type: ignore[misc]
예제 #20
0
 def test_top_level_request_with_preload_content(self) -> None:
     r = request("GET", f"{self.base_url}/echo", preload_content=False)
     assert r.status == 200
     assert r.connection is not None
     r.data
     assert r.connection is None