Python脚本无法在AWS-EC2上持续运行

时间:2019-07-17 09:49:45

标签: python-3.x amazon-web-services amazon-ec2 web3

因此,我想在AWS-EC2上运行一个简单的python脚本,以检查以太坊地址的余额,然后将其全部发送到另一个帐户(如果可用)。我希望它可以无限期运行,而不会造成任何停机或在存入和提取之间有很大的延迟。

但是此代码运行了几个小时/天,然后停止了一段时间,然后以某种方式再次运行,或者只是停止了,所以我无法理解问题。

我已经尝试过使用screen和nohup,但问题仍然存在。我认为它与ssh和超时有关,因为这是我在输出文件中观察到的。

代码:

'''python'''
from web3 import Web3
import time
infura_url = 'https://mainnet.infura.io/v3/b89462f94705433fb5bde725eec8832c'
web3 = Web3(Web3.HTTPProvider(infura_url))
if web3.isConnected() :
    receiver = "xxaddressxx"
    sender = "xxsenderxx"
    pvt_key = "xxprivate keyxx"
    gas = 21000
    while 1 :
        balance = web3.eth.getBalance(sender)
        gas_pric = web3.eth.gasPrice
        cost = gas_pric*gas
        send_val = balance-cost
        if send_val > 0 :
            nonce = web3.eth.getTransactionCount(sender,'pending')
            tx = {
                'nonce' : nonce,
                'to' : receiver,
                'value' : send_val ,
                'gas' : gas,
                'gasPrice' : gas_pric
                }
            try :
                signed_tx = web3.eth.account.signTransaction(tx,pvt_key)
                if web3.eth.getTransaction(signed_tx['hash']) == None :
                    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
                    print(web3.toHex(tx_hash))
                else :
                    receipt = web3.eth.getTransactionReceipt(tx_hash)
                    if receipt != None :
                        if receipt['status'] == 1 :
                            print("transaction complete")
                        else :
                            print("transaction failed")
            except Exception as e: print(e)

        elif balance == cost :
            print("Reduce tx cost!"
        time.sleep(1)

这是我在nohup输出文件中得到的响应:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 379, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 304, in recv_into
    return self.connection.recv_into(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1717, in recv_into
    self._raise_ssl_error(self._ssl, result)
  File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1523, in _raise_ssl_error
    raise WantReadError()
OpenSSL.SSL.WantReadError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "script_transfer.py", line 11, in <module>
    balance = web3.eth.getBalance(sender)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/eth.py", line 109, in getBalance
    [account, block_identifier],
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/manager.py", line 109, in request_blocking
    response = self._make_request(method, params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/manager.py", line 92, in _make_request
    return request_func(method, params)
  File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/formatting.py", line 50, in apply_formatters
    response = make_request(method, params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/gas_price_strategy.py", line 18, in middleware
    return make_request(method, params)
  File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/formatting.py", line 48, in apply_formatters
    response = make_request(method, formatted_params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/attrdict.py", line 18, in middleware
    response = make_request(method, params)
  File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/formatting.py", line 48, in apply_formatters
    response = make_request(method, formatted_params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/normalize_errors.py", line 9, in middleware
    result = make_request(method, params)
  File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/formatting.py", line 50, in apply_formatters
    response = make_request(method, params)
  File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/formatting.py", line 48, in apply_formatters
    response = make_request(method, formatted_params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/middleware/exception_retry_request.py", line 80, in middleware
    return make_request(method, params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/providers/rpc.py", line 68, in make_request
    **self.get_request_kwargs()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/web3/utils/request.py", line 26, in make_post_request
    response = session.post(endpoint_uri, data=data, *args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 316, in recv_into
    if not util.wait_for_read(self.socket, self.socket.gettimeout()):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/wait.py", line 143, in wait_for_read
    return wait_for_socket(sock, read=True, timeout=timeout)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/wait.py", line 104, in poll_wait_for_socket
    return bool(_retry_on_intr(do_poll, timeout))
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/wait.py", line 42, in _retry_on_intr
    return fn(timeout)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/wait.py", line 102, in do_poll
    return poll_obj.poll(t)
KeyboardInterrupt

我要做的就是获得永不停止的服务,检查并转移我的eth,而没有任何明显的滞后!

我还是AWS,ssh和web3的新手,所以我还不知道如何解决与之相关的问题。

0 个答案:

没有答案