从服务器套接字烧瓶python

时间:2019-01-29 07:57:28

标签: python sockets flask websocket serversocket

我从Flask Python构建了一些Web服务。我使用套接字从硬件接收数据。我在尝试使请求模块永不响应时遇到了一些麻烦。 我的代码是这样的

def on_new_client(clientsocket,addr):
    while True:
        # msg = clientsocket.recv(1024)
        dataascii = clientsocket.recv(7024)
        data = binascii.hexlify(dataascii).decode()
        #do some checks and if msg == someWeirdSignal: break:
        print addr, clientsocket, ' >> ', data

    clientsocket.close()

def SendRequest():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('x.x.x.x', 59972)
    return 'connecting to %s port %s' % server_address
    sock.connect(server_address)
    try:
        strcommand = str('534F5323')
        message = strcommand.decode('hex')
        messageascii = binascii.hexlify(message).decode()
        sock.sendall(message)
        return messageascii

    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()

def launchServer():
    s = socket.socket()         # Create a socket object
    host = ('x.x.x.x', 5000) # Get local machine name
    s.bind(host)       
    s.listen(100)
    while True:
       c, addr = s.accept()    
       print 'Got connection from', addr
       thread.start_new_thread(on_new_client,(c,addr))

@app.route('/sendrequest', methods=['GET', 'POST'])
def index():
    ClientIP = ('x.x.x.x', 59972)
    thread.start_new_thread(SendRequest,())
    return ClientIP

if __name__ == "__main__":
    t = threading.Thread(target=launchServer)
    t.daemon = True
    t.start()
    app.run(debug=True, host="x.x.x.x", port="5001")

我在接收到数据后尝试发送on_new_client,模块可以接受我的请求。我不知道如何向route /sendrequest发出请求。

任何帮助或建议,将不胜感激!!谢谢

0 个答案:

没有答案