Python:网络中断管道错误/程序“崩溃”

时间:2019-04-10 17:38:18

标签: python networking server client

我试图用服务器/客户端软件制作一个系统,一个在Raspberry Pi 3B型(服务器)上,另一个在我的台式计算机(客户端)上。它使用语音识别进行客户端内部的输入,我认为这可能是导致此问题的原因。

当我连接到服务器时,一种方法是强制客户端在侦听服务器的响应(创建断开的管道)之前先询问命令,另一种方法是在询问命令时侦听服务器(并没有真正听命令,只是说要命令)

客户代码:

import socket
import threading
import sys

import pyttsx3
import speech_recognition as sr

class Client:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    address = '192.168.1.222'

    def talk(self, message):
        print(message)
        engine.say(message)
        engine.runAndWait()

    def command(self):
        r = sr.Recognizer()

        with sr.Microphone() as source:
            r.pause_threshold = 1
            audio = r.listen(source)

        try:
            command = r.recognize_google(audio, language='en')
        except:
            self.talk('I\'m sorry, I couldn\'t understand!')
            command = ""

        return command

    def run(self):
        while True:
            self.talk('How can I help?')
            com = self.command()
            com = com.lower()
            print(com)

            self.sock.send(bytes(com, 'utf-8'))

            ''' checks data after command has been found (broken pipe caused)
            while True:
                data = self.sock.recv(1024)
                if len(data) > 0:
                    self.talk(data)
                    break
            '''

    def __init__(self):
        self.sock.connect((self.address, 10000))

        iThread = threading.Thread(target=self.run)
        iThread.deamon = True
        iThread.start()

        ''' checks data while getting command (no command recieved)
        while True:
                data = self.sock.recv(1024)
                if len(data) > 0:
                    self.talk(data)
                    break
        '''

engine = pyttsx3.init()
client = Client()

服务器代码:

import socket
import threading

class Server:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        connections = []

        def __init__(self):
                self.sock.bind(('0.0.0.0', 10000))
                self.sock.listen(1)

        def handler(self, c, a):
                while True:
                        data = c.recv(1024)
                        for connection in self.connections:
                                connection.send(bytes(data))
                        if not data:
                                self.connections.remove(c)
                                c.close()
                                break
                        print(data)

        def run(self):

                while True:
                        c, a = self.sock.accept()
                        cThread = threading.Thread(target=self.handler, args=(c,a))
                        cThread.deamon = True
                        cThread.start()

                        self.connections.append(c)
                        print(self.connections)

server = Server()
server.run()

我只希望在获取命令之后(现在无论如何),现在响应,出于测试目的,我只想从服务器返回我已发送的内容(稍后将更改此内容,以便服务器决定返回什么消息),因此,如果我通过麦克风说“你好”,则服务器应该能够返回相同的响应(但以字节为单位)

0 个答案:

没有答案