Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当脚本在办公室的cronjob上运行时,Python才会请求/urllib3 NewConnectionError_Python_Cron_Python Requests_Crontab_Cron Task - Fatal编程技术网

仅当脚本在办公室的cronjob上运行时,Python才会请求/urllib3 NewConnectionError

仅当脚本在办公室的cronjob上运行时,Python才会请求/urllib3 NewConnectionError,python,cron,python-requests,crontab,cron-task,Python,Cron,Python Requests,Crontab,Cron Task,奇怪的问题我搞不懂 我有一个脚本,它使用Python的请求库并在cronjob上运行。当我在家通过VPN时,它工作正常 如果我在办公室,cronjob会返回一个连接错误,更具体地说是一个NewConnectionError[error 60:connection timeout](由urllib3引发)。奇怪的是,如果我从命令行手动运行脚本,它就不会有问题 我对requests/urllib3/cron的工作原理只有很高的理解。我猜连接是以某种方式缓存的,但我不确定。有人知道这是什么原因吗 脚本

奇怪的问题我搞不懂

我有一个脚本,它使用Python的请求库并在cronjob上运行。当我在家通过VPN时,它工作正常

如果我在办公室,cronjob会返回一个连接错误,更具体地说是一个NewConnectionError[error 60:connection timeout](由urllib3引发)。奇怪的是,如果我从命令行手动运行脚本,它就不会有问题

我对requests/urllib3/cron的工作原理只有很高的理解。我猜连接是以某种方式缓存的,但我不确定。有人知道这是什么原因吗

脚本本身是一个同步实用程序,用于创建到bitbucket api的连接。我创建了一个api包装器来实现这一点,它本质上只是一个用来构建查询的对象。以下是包装器中的一个片段:

def __init__(self, username, password):
    s = requests.Session()
    s.auth = (username, password)
    self._bitbucket_session = s

def _get_context(self, url, paging):
    try:
        r = self._bitbucket_session.get(url)
        if r.status_code == 403:
            raise self.BitbucketAPIError('BitbucketAPIError: {}'.format(r.reason))
        if 'error' in r.json():
            raise self.BitbucketAPIError('BitbucketAPIError: {}'.format(r.json()['error']['message']))
    except HTTPError as e:
        print("HTTP Error: {}".format(e))
    except ConnectTimeout as e:
        print("The request timed out while trying to connect to the remote server: {}".format(e))
    except ConnectionError as e:
        print("Connection Error: {}".format(e))
    except Timeout as e:
        print("Connection Timed out: {}".format(e))
    except RequestException as e:
        print("Unhandled exception: {}".format(e))
下面是一个正在“croned”的同步客户端的简化版本:

以下是关闭方法:

def close(self):
    self._bitbucket_session.close()

可能涉及到一个代理

当脚本在家中运行时,没有代理,或者代理配置正确,因此没有问题

从办公室的命令行运行时,shell环境已正确配置为通过环境变量设置HTTP/S代理:

export http_proxy="http://proxy.com:3128"
export https_proxy="https://proxy.com:3128"
(大写变量也有效,即HTTP\U代理、HTTPS\U代理)

但是,当脚本从
cron
运行时,环境没有设置代理变量,连接请求超时。您为脚本创建一个包装器,然后从cron执行包装器脚本。e、 g

#!/bin/sh
export HTTP_PROXY="http://proxy:1234"
export HTTPS_PROXY="https://proxy:1234"
python your_script.py

这个cron作业是在你从家里带到办公室的笔记本电脑上运行的吗?@Hugh,你确定吗?当我以我自己的身份设置cron作业时,它们以我自己的身份运行。否则这将是一个巨大的安全漏洞,不是吗?@JohnGordon:你说得对;我在考虑系统cron作业,除非指定以用户身份运行,否则默认为root。这很有道理,我只通过.bash\u profile设置这些变量。凉豆。我今晚会把它设置好,明天回去报到时再测试。谢谢你,先生。
#!/bin/sh
export HTTP_PROXY="http://proxy:1234"
export HTTPS_PROXY="https://proxy:1234"
python your_script.py