子过程导致“管道破裂”错误

时间:2019-09-21 16:00:15

标签: python-3.x sockets subprocess

我正在玩套接字编程。在下面的代码段中,我尝试连接到客户端,如果他的输入包含“ hack”,它将删除它并运行shell命令并发回输出。

服务器端:

import socket

class SP:

    def server(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind(('127.0.0.1', 9999))
            s.listen(1)
            while True:
                try:
                    c, addr = s.accept()
                    print('Got connection from ', addr)
                    while True:
                        data = c.recv(1024)
                        if data:
                            if 'hack' in data.decode('utf-8'):
                                import subprocess
                                data = data.decode('utf-8')
                                data = data.strip('hack').lstrip().rstrip()
                                output = subprocess.call(data, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                                s.send(str(output).encode('utf-8'))

                            else:
                                d = data.decode('utf-8')
                                print('Got data: '+str(d))
                                c.send(str('ACK: '+str(d)+' ...').encode('utf-8'))

                        else:
                            print('No more data from client: '+str(addr))
                            break
                finally:
                    s.close()
        except Exception as e:
            print('Caught Exception: '+str(e))
            s.close()

obj = SP()
obj.server()

客户端:

import socket

class CS:

    def client(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 9999))
            while True:
                data = input('Enter data to be sent to server: \n')
                if not data:
                    break
                else:
                    s.send(data.encode('utf-8'))
                    reply = s.recv(1024).decode('utf-8')
                    print(str(reply))
            else:
                s.close()
        except Exception as e:
            print('Caught Exception: '+ str(e))
            s.close()

obj = CS()
obj.client()

如何解决此错误? Caught Exception: [Errno 32] Broken pipe并没有告诉我很多。

更新:

import socket

class SP:

    def server(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind(('127.0.0.1', 9999))
            s.listen(1)
            while True:
                try:
                    c, addr = s.accept()
                    print('Got connection from ', addr)
                    while True:
                        data = c.recv(1024)
                        if data:

                            if 'hack' in data.decode('utf-8'):
                                import subprocess
                                data = data.decode('utf-8')
                                data = data.strip('hack').lstrip().rstrip()
                                print(data)
                                #output = subprocess.call(data, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                                s.send(data.encode('utf-8'))

                            else:
                                d = data.decode('utf-8')
                                print('Got data: '+str(d))
                                c.send(str('ACK: '+str(d)+' ...').encode('utf-8'))

                        else:
                            print('No more data from client: '+str(addr))
                            break
                finally:
                    s.close()
        except Exception as e:
            print('Caught Exception: '+str(e))
            s.close()

obj = SP()
obj.server()

即使我在呼叫subprocess.call的行中注释掉,我仍然会收到“管道中断”的信息,因此错误并非源自subprocess调用。

1 个答案:

答案 0 :(得分:0)

您正在使用服务器的套接字s而不是客户端的套接字c将数据发送到客户端:
s.send(data.encode('utf-8'))

如何将其更改为:
c.send(data.encode('utf-8'))