Eri*_*mpo 5 linux cron debian python-3.x python-requests
我有一个Python脚本,可通过HTTP以json格式下载数据。如果我使用请求模块通过命令行运行脚本,则HTTP连接成功,并且下载数据没有任何问题。但是,当我尝试将脚本作为crontab作业启动时,HTTP连接会在一段时间后引发超时。谁能告诉我这是怎么回事?我目前正在首先通过bash脚本下载数据,然后从该bash中运行Python脚本。但这是胡说!非常感谢!
使用:3.6.1 | Anaconda自定义(64位)| (默认值,2017年5月11日,13:09:58)\ n [GCC 4.4.7 20120313(Red Hat 4.4.7-1)]
PS:我还没有找到有关此问题的任何帖子。如果在其他帖子上已经对此有答案,请接受我的歉意。
这是我的代码的摘录。运行request.get(url)时超时:
try:
response = requests.get(url)
messages = response.json()["Messages"]
except requests.exceptions.Timeout:
logging.critical("TIMEOUT received when connecting to HTTP server.")
except requests.exceptions.ConnectionError:
logging.critical("CONNECTION ERROR received when connecting to HTTP server.")
Run Code Online (Sandbox Code Playgroud)
我刚刚找到了我的问题的答案。我已经定义了正在使用的代理,然后在我的代码中像这样使用它:
HTTP_PROXY="http://your_proxy:proxy_port"
PROXY_DICT={"http":HTTP_PROXY}
response = requests.get(url, proxies=PROXY_DICT)
Run Code Online (Sandbox Code Playgroud)
参考:
感谢大家的理解。我想我应该在发帖之前进行彻底的搜索。对不起。
| 归档时间: |
|
| 查看次数: |
542 次 |
| 最近记录: |