没有关闭套接字连接,想要使用bind()

时间:2017-02-24 02:33:20

标签: python sockets bind

我在python2.7中使用websocket-client并为websocket交互打开了许多连接。

from websocket import create_connection
import json

def openTick():
    signal = 0
    bfnxWS = "wss://api.bitfinex.com/ws"
    ws = create_connection(bfnxWS)

    ws.send(json.dumps({
        "event": "subscribe",
        "channel": "ticker",
        "pair": "BTCUSD",
    }))

    while True:
        result = ws.recv()
        result = json.loads(result)
        #print result
        for index, i in enumerate(result):
            if index == 1:
                if type(i) != unicode:
                    return i

    ws.close()

在我的main函数中多次调用此交互,每次打开一个新连接。

我在youtube和其他模块的教程中读过关于bind()的内容。但是在这个模块中找不到实现它的方法。

这似乎很简单,我不想换成高速公路或其他东西。

目标是使用相同的连接。一旦我创建它,任何人都可以给我一些关于使用相同连接的提示。现在,打开的连接正在我的机器上累积,导致错误被抛出。

这是显示累积连接的netstat输出...

tcp        0      0 neo:47116              104.16.173.181:https    ESTABLISHED


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    ESTABLISHED


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    ESTABLISHED
tcp        0      0 neo:55812              104.16.172.181:https    ESTABLISHED
tcp        0      0 neo:59666              104.16.171.181:https    ESTABLISHED


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:59666              104.16.171.181:https    ESTABLISHED


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    ESTABLISHED
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:59666              104.16.171.181:https    ESTABLISHED
tcp        0     96 neo:47130              104.16.173.181:https    ESTABLISHED
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp       49      0 neo:59666              104.16.171.181:https    ESTABLISHED
tcp        0      0 neo:47130              104.16.173.181:https    ESTABLISHED
tcp        0      0 neo:55822              104.16.172.181:https    ESTABLISHED
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:59676              104.16.171.181:https    TIME_WAIT  


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:48008              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55822              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:59676              104.16.171.181:https    TIME_WAIT  


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0    126 neo:48490              104.16.174.181:https    ESTABLISHED
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:47140              104.16.173.181:https    ESTABLISHED
tcp        0      0 neo:48008              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55822              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:59676              104.16.171.181:https    TIME_WAIT  


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:48490              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:47140              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48008              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55822              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:55832              104.16.172.181:https    ESTABLISHED
tcp        0      0 neo:59686              104.16.171.181:https    TIME_WAIT  
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:59676              104.16.171.181:https    TIME_WAIT  


tcp        0      0 neo:47116              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48470              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:48490              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:47120              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:47998              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55812              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:47140              104.16.173.181:https    TIME_WAIT  
tcp        0      0 neo:48008              104.16.175.181:https    TIME_WAIT  
tcp        0      0 neo:55822              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:48500              104.16.174.181:https    ESTABLISHED
tcp        0      0 neo:55832              104.16.172.181:https    TIME_WAIT  
tcp        0      0 neo:59686              104.16.171.181:https    TIME_WAIT  
tcp        0      0 neo:48480              104.16.174.181:https    TIME_WAIT  
tcp        0      0 neo:59676              104.16.171.181:https    TIME_WAIT

如何绑定接收端口以便我不会累积连接或如何有效地关闭()连接?

0 个答案:

没有答案