Demo entry 6624394

python test code

   

Submitted by anonymous on Jun 16, 2017 at 02:10
Language: Python 3. Code size: 1.3 kB.

from socket import *
import threading
import speechRecognizer

BUFFSIZE = 1024
HOST = '192.168.0.98'
PORT = 5000
FILE_NUM = 0


def response(key):
    return 'Server response : ' + key


def handler(clientSocket, addr):
    global FILE_NUM
    FILE_NUM += 1
    FILE_NAME = "hello" + str(FILE_NUM).rjust(4, '0') + ".raw"
    f = open(FILE_NAME, 'wb')
    while True:
        data = clientSocket.recv(BUFFSIZE)
        if not data:
            break

        f.write(data)
        #print('recv :', repr(data))
        #clientSocket.send(response(data))

        #print('sent :', repr(response(data)))
        if 'close' == data.rstrip(): break

    print('file write is done : ', FILE_NAME)

    recog = speechRecognizer.Recognizer()
    result = recog.google_recognizer(FILE_NAME)
    print(result)

    #clientSocket.close()
    print('close connection : ', addr)


if __name__ == '__main__':
    ADDR = (HOST, PORT)
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serverSocket.bind(ADDR)
    serverSocket.listen(5)
    while True:
        print('waiting for connection...')
        clientSocket, addr = serverSocket.accept()
        print('connected from ', addr)
        threading._start_new_thread(handler, (clientSocket, addr))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).