Advertisement
Guest User

NCJ-Client.py

a guest
Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/python3.6
  2.  
  3. import socket
  4. import sys
  5. import time
  6.  
  7. HOST = "127.0.0.1"
  8. PORT = None
  9.  
  10. def send_file_to_server():
  11.     filepath = input("Please enter the name of a file you want to send.\n>")
  12.  
  13.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  14.  
  15.         sock.connect((HOST, PORT))
  16.        
  17.         req = 'GET {}'.format(filepath)
  18.         sock.sendall(bytes(req, 'utf8'))
  19.  
  20.         filedata = b""
  21.         framedata = True
  22.         while framedata:
  23.             framedata = sock.recv(1024)
  24.             filedata += framedata
  25.  
  26.         with open("recv-" + filepath, "wb+") as fp:
  27.             fp.write(filedata)
  28.    
  29.     print("Finished recieving and writing file to recv-{}".format(filepath))
  30.  
  31.     sock.shutdown(socket.SHUT_WR)
  32.  
  33.  
  34. if __name__ == "__main__":
  35.     if len(sys.argv) < 2:
  36.         print("Please specify server port...\nExiting")
  37.         exit(1)
  38.     portStr = sys.argv[1]
  39.     try:
  40.         PORT = int(portStr)
  41.         send_file_to_server()
  42.     except Exception as e:
  43.         print(e)
  44.  
  45.  
  46. #https://realpython.com/python-sockets/
  47. #https://www.w3resource.com/python/python-bytes.php
  48. #https://stackoverflow.com/questions/27241804/sending-a-file-over-tcp-sockets-in-python
  49. #https://stackoverflow.com/questions/18994912/ending-an-infinite-while-loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement