【发布时间】:2015-02-08 01:06:49
【问题描述】:
我在 Amazon EC2 服务器上有一个 python 脚本,它从两个不同的服务器(使用 urllib 和 http.request)请求数据,然后将该数据记录在一个文本文件中。它必须运行很长时间。我正在使用 nohup 让它在后台运行。
问题是,它会在一段时间后停止(有时会持续 24 小时,有时会持续 2 小时,情况会有所不同)。我没有收到错误消息或任何东西。它只是停止并且收到的最后一串字符只是作为不完整的字符串保存在文本文件中(只是可以从远程服务器读取的信息)。
什么可能导致这个问题?
这是我的代码:
import urllib3 # sudo pip install urllib3 --upgrade
import time
urllib3.disable_warnings() # Disable urllib3 warnings about unverified connections
http = urllib3.PoolManager()
f = open('okcoin.txt', 'w')
f2 = open('bitvc.txt', 'w')
while True:
try:
r = http.request("GET","https://www.okcoin.com/api/v1/future_ticker.do?symbol=btc_usd&contract_type=this_week")
r2 = http.request("GET","http://market.bitvc.com/futures/ticker_btc_week.js ")
except: # catch all exceptions
continue
#Status codes of 200 if it got an OK from the server
if r.status != 200 or r2.status != 200 or r.data.count(',') < 5 or r2.data.count(',') < 5: # avoids blank data, there should be at least 5 commas so that it's correct data
continue; # Try to read again if there was a problem with one reading
received = str(time.time()) # Timestamp of when the information was received to the server running this python code
data = r.data + "," + received + "\r\n"
data2 = r2.data + "," + received + "\r\n"
print data,r.status
print data2, r.status
f.write(data)
f2.write(data2)
time.sleep(0.5)
f.flush() #flush files
f2.flush()
f.close()
f2.close()
编辑:我通过 ssh 使用屏幕打开了程序。它又停了下来。如果我按“CTRL+C”停止它,这就是我得到的:
^CTraceback (most recent call last):
File "tickersave.py", line 72, in <module>
r2 = http.request("GET","http://market.bitvc.com/futures/ticker_btc_week.js")
File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 81, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/local/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 153, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 541, in urlopen
**response_kw)
File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 284, in from_httplib
**response_kw)
File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 104, in __init__
self._body = self.read(decode_content=decode_content)
File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 182, in read
data = self._fp.read()
File "/usr/lib/python2.7/httplib.py", line 551, in read
s = self._safe_read(self.length)
File "/usr/lib/python2.7/httplib.py", line 658, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
有什么线索吗?我应该添加超时还是什么?
【问题讨论】:
标签: python amazon-ec2 nohup http-request