快速套接字在python中关闭?

时间:2016-08-10 18:11:34

标签: python sockets

我试图在循环中运行一个小脚本..应该做的是打开一个套接字,接受命令,运行命令,发送'完成到客户端',关闭套接字..然后再次打开它以进行新的会话。这是我运行的脚本的一个简单例子;

def Tcp_connect( HostIp, Port ):
    global s
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HostIp, Port))
    return

def Tcp_server_wait ( numofclientwait, port ):
        global s2
        s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s2.bind(('',port))
        s2.listen(numofclientwait)

def Tcp_server_next ( ):
                global s
                s = s2.accept()[0]

def Tcp_Write(D):
   s.send(D + '\r')
   return

def Tcp_Read( ):
        a = ' '
        b = ''
        while a != '\r':
                a = s.recv(1)
                b = b + a
        return b

def Tcp_Close( ):
   s.close()
   return




while True:

    Tcp_server_wait ( 5, 17098 )
    Tcp_server_next()
    data=Tcp_Read()
    print data #Eventually do some stuff here
    Tcp_Write('Done')
    Tcp_Close()
    time.sleep(1)

当我运行它时它工作了一次,然后我得到了;

socket.error: [Errno 98] Address already in use

这是我在最后结束时......有什么想法吗?

0 个答案:

没有答案