realsdx

FTP_SERVFER

Mar 4th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import socket
  2. import os,sys
  3.  
  4. ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  5. ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  6. addr=('localhost',4444)
  7. ss.bind(addr)
  8. ss.listen(5)
  9.  
  10. def main():
  11.     while True:
  12.         try:
  13.             print("Server waiting for connection ..")
  14.             cs,c_addr=ss.accept()
  15.             print("Client connected from:",c_addr)
  16.             file_name=cs.recv(1024).decode()
  17.             if os.path.exists(file_name):
  18.                 print("File is in server sending it to client.")
  19.  
  20.                 file=open(file_name,'rb')
  21.                 while True:
  22.                     data=file.read(4096)
  23.                     if not data:
  24.                         break
  25.                     cs.send(data)
  26.                     print("Sending...",end="\r")
  27.  
  28.                 print("Sent.")
  29.                 cs.close()
  30.             else:
  31.                 print("Receiving file:%s"%file_name)
  32.                 #input("Do you want to download it? Press Return to continue or CTRL-D to exit.")
  33.                 rcv_file=open(file_name,'wb')
  34.                
  35.                 while True:
  36.                     data=cs.recv(4096)
  37.                     if not data:
  38.                         break
  39.                     print("Receiving...",end="\r")
  40.                     rcv_file.write(data)
  41.  
  42.                 print("\nRecived")
  43.                 cs.close()
  44.         except KeyboardInterrupt:
  45.             ss.close()
  46.             sys.exit(1)
  47.  
  48.     ss.close()
  49.  
  50. main()
Add Comment
Please, Sign In to add comment