realsdx

FTP_CLIENT

Mar 4th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import socket
  2. import os,sys,time
  3.  
  4. cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  5. cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  6. print("Socket Created...")
  7.  
  8. port=4444
  9.  
  10. cs.connect(('localhost',port))
  11. print("Connected to port %s"%str(port))
  12. time.sleep(0.2)
  13.  
  14. def main():
  15.     op =  input("Enter choice:\n 1. Upload 2. Download :>>")
  16.     op = int(op)
  17.     if op == 1:
  18.         file_path=input("Enter the file name or full path:")
  19.         file_abspath=os.path.abspath(file_path)
  20.         file_name=os.path.basename(file_path)
  21.         cs.send(file_name.encode())
  22.  
  23.         print("File transfer starting...")
  24.         file=open(file_abspath,'rb')
  25.        
  26.         while True:
  27.             data=file.read(4096)
  28.             if not data:
  29.                 break
  30.             cs.send(data)
  31.             print("Sending...",end="\r")
  32.  
  33.  
  34.         cs.close()
  35.         print("Done sending")
  36.  
  37.     elif op ==2:
  38.         file_name=input("Enter the file name to download:")
  39.         cs.send(file_name.encode('utf8'))
  40.  
  41.         print("File Download starting...")
  42.         rcv_file=open(file_name,'wb')
  43.        
  44.         while True:
  45.             data=cs.recv(4096)
  46.             if not data:
  47.                 break
  48.             print("Receiving...",end="\r")
  49.             rcv_file.write(data)
  50.  
  51.  
  52.         cs.close()
  53.         print("Done downloading")
  54.  
  55.  
  56. main()
Add Comment
Please, Sign In to add comment