Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3.6
- import socket
- import sys
- import time
- HOST = "127.0.0.1"
- PORT = None
- def send_file_to_server():
- filepath = input("Please enter the name of a file you want to send.\n>")
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
- sock.connect((HOST, PORT))
- req = 'GET {}'.format(filepath)
- sock.sendall(bytes(req, 'utf8'))
- filedata = b""
- framedata = True
- while framedata:
- framedata = sock.recv(1024)
- filedata += framedata
- with open("recv-" + filepath, "wb+") as fp:
- fp.write(filedata)
- print("Finished recieving and writing file to recv-{}".format(filepath))
- sock.shutdown(socket.SHUT_WR)
- if __name__ == "__main__":
- if len(sys.argv) < 2:
- print("Please specify server port...\nExiting")
- exit(1)
- portStr = sys.argv[1]
- try:
- PORT = int(portStr)
- send_file_to_server()
- except Exception as e:
- print(e)
- #https://realpython.com/python-sockets/
- #https://www.w3resource.com/python/python-bytes.php
- #https://stackoverflow.com/questions/27241804/sending-a-file-over-tcp-sockets-in-python
- #https://stackoverflow.com/questions/18994912/ending-an-infinite-while-loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement