简体   繁体   English

没有属性 response.status_code 的 grequests

[英]grequests not having the attributes response.status_code

Can I get a tip for how to do an http request via a VOLTTRON agent using grequests?我可以通过VOLTTRON 代理使用 grequests 获得有关如何执行 http 请求的提示吗? From what I know about VOLTTRON I think grequests are required for an asynchronous methods.根据我对 VOLTTRON 的了解,我认为异步方法需要 grequests。

Small snip from my VOLTTRON agent code, line 140 on git gist :来自我的 VOLTTRON 代理代码的小片段, 第 140 行 git 要点

    _log.info(f'*** [grequests INFO] *** -  onstart sucess!')
    self.core.schedule(cron('*/1 * * * *'), self.cron_function)


def cron_function(self):
    def simple_request(url):
        page = requests.get(url)
        return page

    _log.info(f'*** [grequests INFO] *** - starting CRON Processes')


    request = grequests.get(simple_request(self.url1))
    _log.info(f'*** [grequests INFO] *** - sending out {request}')

    response = grequests.send(request)

    _log.info(f'*** [grequests INFO] *** - response code {response.status_code}')
    _log.info(f'*** [grequests INFO] *** - response text {response.content}')

The traceback when I install the agent has to do with grequests not having the attributes response.status_code .我安装代理时的回溯与不具有属性response.status_code的 grequests 有关。 Should I be wrapping the requests library with a grequests to make my script asynchronous?我应该用grequests包装requests库以使我的脚本异步吗? Sorry not a lot wisdom here but I only need to do one http request and I think I realize my agent code needs to asynchronous to accommodate what all else is happening on the VOLTTRON platform.抱歉,这里没有太多智慧,但我只需要做一个 http 请求,我想我意识到我的代理代码需要异步以适应 VOLTTRON 平台上发生的所有其他事情。

2021-05-02 10:28:10,875 (grequesteragent-0.1 39693) <stderr> ERROR: Traceback (most recent call last):
2021-05-02 10:28:10,875 (grequesteragent-0.1 39693) <stderr> ERROR:   File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
2021-05-02 10:28:10,875 (grequesteragent-0.1 39693) <stderr> ERROR:   File "/home/dan/Desktop/volttron/volttron/platform/vip/agent/core.py", line 455, in wrapper
2021-05-02 10:28:10,875 (grequesteragent-0.1 39693) <stderr> ERROR:     event.function(*event.args, **event.kwargs)
2021-05-02 10:28:10,876 (grequesteragent-0.1 39693) <stderr> ERROR:   File "/home/dan/.volttron/agents/5b1bc704-ff6e-4d3d-a186-d5ec6b348686/grequesteragent-0.1/grequester/agent.py", line 153, in cron_function
2021-05-02 10:28:10,877 (grequesteragent-0.1 39693) <stderr> ERROR:     _log.info(f'*** [grequests INFO] *** - response code {response.status_code}')
2021-05-02 10:28:10,877 (grequesteragent-0.1 39693) <stderr> ERROR: AttributeError: 'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'status_code'
2021-05-02 10:28:10,877 (grequesteragent-0.1 39693) <stderr> ERROR: 2021-05-02T15:20:00Z <Greenlet at 0x7f44a146c9d0: wrapper> failed with AttributeError
2021-05-02 10:28:10,877 (grequesteragent-0.1 39693) <stderr> ERROR:

Example code from the Ecobee driver that I've annotated:我注释过的来自 Ecobee 驱动程序的示例代码:

def call_grequest(method_name, url, **kwargs):
    """
    Make grequest calls to remote api
    :param method_name: method type - put/get/delete
    :param url: http URL suffix
    :param kwargs: Additional arguments for http request
    :return: grequest response
    """
    try:
        # Get the correct grequests method for the HTTP request type
        fn = getattr(grequests, method_name)
        # use that method with the url and any args to send the request
        request = fn(url, **kwargs)
        # use map to pull out the response
        response = grequests.map([request])[0]
        if response and isinstance(response, list):
            response = response[0]
        # handle cases were we receive a non-200 response
        response.raise_for_status()
        return response
    # handle specific error cases by logging a message and re-raising so we can handle it elsewhere
    except (ConnectionError, NewConnectionError) as e:
        _log.error(f"Error connecting to {url} with args {kwargs}: {e}")
        raise e


def make_ecobee_request(request_type, url, **kwargs):
    """
    Wrapper around making arbitrary GET and POST requests to remote Ecobee API
    :return: Ecobee API response using provided request content
    """
    # Generate appropriate grequests object
    # HTTP request method
    if request_type.lower() in ["get", "post"]:
        # we use verify so we can work with SSL
        response = call_grequest(request_type.lower(), url, verify=requests.certs.where(), timeout=30, **kwargs)
    else:
        raise ValueError(f"Unsupported request type {request_type} for Ecobee driver.")
    # Send request and extract data from response
    headers = response.headers
    # parse JSON content
    if "json" in headers.get("Content-Type"):
        return response.json()
    else:
        # parse Text content
        content = response.content
        if isinstance(content, bytes):
            content = jsonapi.loads(response.decode("UTF-8"))
        return content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM