简体   繁体   English

Python-请求模块-接收流更新-对等重置连接

[英]Python - Requests module - Receving streaming updates - Connection reset by peer

I have been building my own python (version 3.2.1) trading application in a practice account of a Forex provider (OANDA) but I am having some issues in receiving the streaming prices with a Linux debian-based OS. 我已经在一个外汇提供商(OANDA)的实践帐户中构建了自己的python(3.2.1版)交易应用程序,但是在使用基于Linux debian的OS接收流价格时遇到了一些问题。

In particular, I have followed their "Python streaming rates" guide available here: http://developer.oanda.com/rest-live/sample-code/ . 特别是,我遵循了他们的“ Python流传输率”指南,可在这里找到: http : //developer.oanda.com/rest-live/sample-code/

I have a thread calling the function 'connect_to_stream' which prints out all the ticks received from the server: 我有一个线程调用函数“ connect_to_stream”,该函数打印出从服务器接收到的所有滴答声:

streaming_thread = threading.Thread(target=streaming.connect_to_stream, args=[])
streaming_thread.start()

The streaming.connect_to_stream function is defined as following: stream.connect_to_stream函数的定义如下:

def connect_to_stream():  

   [..]#provider-related info are passed here

    try:
        s = requests.Session()
        url = "https://" + domain + "/v1/prices"
        headers = {'Authorization' : 'Bearer ' + access_token,
                   'Connection' : 'keep-alive'
                  }
        params = {'instruments' : instruments, 'accountId' : account_id}
        req = requests.Request('GET', url, headers = headers, params = params)
        pre = req.prepare()
        resp = s.send(pre, stream = True, verify = False)
        return resp
    except Exception as e:
        s.close()
        print ("Caught exception when connecting to stream\n%s" % str(e))

    if response.status_code != 200:
            print (response.text)
            return
    for line in response.iter_lines(1):
        if line:
            try:
                msg = json.loads(line)
                print(msg)
            except Exception as e:
                print ("Caught exception when connecting to stream\n%s" % str(e))
                return

The msg variable contains the tick received for the streaming. msg变量包含为流式传输收到的滴答声。

The problem is that I receive ticks for three hours on average after which the connection gets dropped and the script either hangs without receiving any ticks or throws an exception with reason "Connection Reset by Peer". 问题是,我平均会收到三个小时的滴答声,然后断开连接,脚本会挂起而没有收到任何滴答声,或者抛出异常,原因是“对等连接重置”。

Could you please share any thoughts on where I am going wrong here? 您能否对我在这里出什么问题分享任何想法? Is it anything related to the requests library (iter_lines maybe)? 它与请求库(可能是iter_lines)有关吗?

I would like to receive ticks indefinitely unless a Keyboard exception is raised. 除非引发键盘异常,否则我希望无限期收到报价。

Thanks 谢谢

That doesn't seem too weird to me that a service would close connections living for more than 3 hours. 对于我来说,服务关闭关闭超过3小时的连接似乎并不奇怪。

That's probably a safety on their side to make sure to free their server sockets from ghost clients. 确保从幽灵客户端释放服务器套接字对他们而言可能是安全的。

So you should probably just reconnect when you are disconnected. 因此,您可能应该在断开连接时重新连接。

try:
    s = requests.Session()
    url = "https://" + domain + "/v1/prices"
    headers = {'Authorization' : 'Bearer ' + access_token,
               'Connection' : 'keep-alive'
              }
    params = {'instruments' : instruments, 'accountId' : account_id}
    req = requests.Request('GET', url, headers = headers, params = params)
    pre = req.prepare()
    resp = s.send(pre, stream = True, verify = False)
    return resp
except SocketError as e:
    if e.errno == errno.ECONNRESET:
        pass # connection has been reset, reconnect.
except Exception as e:
    pass # other exceptions but you'll probably need to reconnect too.

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

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