2017-12-05 1 views
0

私はコンピュータサイエンスのクラスで、リモートサーバーにファイルを送信できるものを作っていますが、それはうまく動作しますが、recvしません。私はserver.pyが問題pythonでファイルを送受信する

クライアントの.py

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.connect(('139.59.173.187',8000)) 
    print("=========================Welcome to the File System =========================") 
    option = input("Here are your options:\n--> Press S to upload files\n--> Press R to retrieve files") 
    if option == "S" or option == "s": 
     s.send("S".encode('utf-8')) 
     file_name = input("Please input the file name\n>>>") 
     name = input("What would you like it to be saved as?\n>>>") 

     s.send(name.encode('utf-8')) 

     with open(file_name,'rb') as f: 
      s.sendall(f.read()) 
     print("File has been sent to the server") 
     main() 

    elif option == "R" or option == "r": 
     s.send("R".encode('utf-8')) 
     filename = input("What is the filename you wish to recieve\n>>>") 
     s.send(filename.encode('utf-8')) 

     print ("File is comming ....") 
     f = open(filename,'wb')  #open that file or create one 
     l = c.recv(1024)   #now recieves the contents of the file 
     while (l): 

      print ("Receiving...File Data") 
      f.write(l)   #save input to file 
      l = c.recv(1024)  #get again until done 
     print("The file",filename,"has been succesfully saved to your computer") 
     f.close() 

だと思う SERVER.PY

import socket # Import socket module 
import os #imports os module 
def RecvFile(): 
    while True: # allows it to always save files and not have to be restarted. 
     c, addr = s.accept() 
     print ('Connection Accepted From',addr)# this prints out connection and port 
     print ("File is comming ....") 
     file = c.recv(1024)  #recieves file name from client 


     f = open(file,'wb')  #open that file or create one 
     l = c.recv(1024)   #now recieves the contents of the file 
     while (l): 

      print ("Receiving...File Data") 
      f.write(l)   #save input to file 
      l = c.recv(1024)  #get again until done 
     print("The file",file,"has been succesfully saved to the server\nConnection from:",addr) 
     f.close() 

def SendFile(): 
    c, addr = s.accept() 
    print("Connection Accepted From",addr) 
    filename = c.recv(1024) 
    if os.path.isfile(filename): 
     s.send("EXISTS"+str(os.pthat.getsize(filename))) 
     response = c.recv(1024) 
     if response[:2] == 'OK': 
      with open(filename, 'rb') as f: 
       s.sendall(f.read) 
      f.close() 
    else: 
     s.send("ERR") 
    s.close() 

def main(): 
    s = socket.socket() 
    s.bind(('139.59.173.187', 8000)) # Binds port and IP address 
    s.listen(3) 

    print("Server Started") 
    while True: 
     c, addr = s.accept() 
     option = c.recv(1024) 
     if option == "S": 
      RecvFile() 
     elif option == "R": 
      SendFile() 
     else: 
      s.send("Incorrect input".encode('utf-8')) 


main() 

#if errors occur 
c.close() 

は、私はこれを学習に7時間を入れているとasignmentは、金曜日に予定です。どのように動作するかをコメントすることができるでしょうか?

+0

あなたが特定のエラーを取得していますか? –

+0

エラーは壊れたパイプですが、私はママがそれを修正したと思います –

答えて

0

server.pyでは、SendFileRecvFileはどちらもアクセスできないソケットオブジェクトsを使用しています。私はあなたがそれを引数として渡すことをお勧めします:メインで

def RecvFile(s): 
#... 

def SendFile(s): 
#... 

そして:

if option == "S": 
    RecvFile(s) 
elif option == "R": 
    SendFile(s) 
関連する問題