Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- import sys
- import getopt
- import socket
- def main(argv):
- """Parse command line arguements to transfer file"""
- #Attempt to parse command line args, catch error
- try:
- options, arguements = getopt.getopt(argv, "a:f:hp:svw", ["address=", "file=", "help", "port=", "send", "verbose", "wait"])
- except getopt.GetoptError:
- error("Incorrect arguements")
- #Parse getopt option, arguement tuples to a dictionary
- args = dict(options)
- #Store whether the command should be verbose
- global verbose_bool
- verbose_bool = "-v" in args.keys() or "--verbose" in args.keys()
- #Test for presence of a wait and/or send flag
- wait_bool = "-w" in args.keys() or "--wait" in args.keys()
- send_bool = "-s" in args.keys() or "--send" in args.keys()
- #Resolve script action from flags
- if wait_bool and send_bool:
- error("Cannot wait and send.")
- elif wait_bool:
- #Wait on the specified port
- port = args.get("-p") or args.get("--port")
- if port is None:
- error("Must specify port to wait on")
- wait(port)
- elif send_bool:
- #Retrieve the target address
- address = args.get("-a") or args.get("--address")
- if address is None:
- error("Must specify address to send to.")
- #Retrive port to transfer on
- port = args.get("-p") or args.get("--port")
- if port is None:
- error("Must specify port to transfer on")
- #Retrieve the filename
- filename = args.get("-f") or args.get("--file")
- if filename is None:
- error("Must specify file to send")
- #Send the file to the adress
- send(address, port, filename)
- elif "-h" in args.keys() or "--help" in args.keys():
- #Display usage and exit
- usage()
- sys.exit(0)
- else:
- error("Must send, wait, or help")
- def wait(port):
- """Wait for a transfer on specified port"""
- print ("Waiting for connection on port", port + "...")
- #Establish server socket
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Set up server socket
- server.bind(('', int(port))) #Bind socket to specified port
- server.listen(1) #Listen for up to 1 connection on bound port
- connection, address = server.accept() #On connection accept, store new connection and address
- print ("Connected to address", address)
- #Recieve the data
- if verbose_bool: print ("\nRecieving data")
- data = ""
- while True:
- new_data = connection.recv(512)
- if not new_data:
- break
- data += new_data
- if verbose_bool: print ("Data recieved")
- #Seperate filename and file content via delimeter
- delim_index = data.find("///")
- delim_end = delim_index + 3
- filename = data[:delim_index]
- file_content = data[delim_end:]
- #Save file
- if verbose_bool: print ("\nSaving file", filename)
- f = open(filename, "w")
- f.write(file_content)
- f.close()
- print ("File", filename, "saved")
- #Close connection"2
- if verbose_bool: print ("\nClosing connection")
- connection.close()
- def send(address, port, filename):
- """Send a file to an address on specified port"""
- print ("Establishing connection on port", port)
- #Establish client socket
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Set up client socket
- client.connect((address, int(port)))
- print ("Connection established")
- #Read in file
- if verbose_bool: print ("\nReading file")
- try:
- f = open(filename, "r")
- except IOError:
- print ("Error: File", filename, "not found")
- client.close()
- sys.exit(1)
- file_content = f.read()
- f.close()
- if verbose_bool: print ("File read")
- #Format data for sending
- data = filename + "///" + file_content
- #Send data
- if verbose_bool: print ("\nSending", filename)
- while data:
- send_data = data[:512]
- data = data[512:]
- client.send(send_data)
- print ("Sent", filename)
- #Close client connection
- if verbose_bool: print ("\nClosing connection")
- client.close()
- def error(msg):
- """Print an error message and usage, then quit"""
- print (msg, "\n")
- usage()
- sys.exit(2)
- def usage():
- """Print script usage"""
- print ("Usage: sendfile [OPTION] ...")
- print ("Sends a file to another sendfile user on the other end.")
- print ("")
- print ("-a, --address The adress to send to ")
- print ("-f, --file Filename to send")
- print ("-h, --help Print this usage and quit")
- print ("-p, --port The port to send or wait on")
- print ("-s, --send Flag telling sendfile to send")
- print ("-w, --wait Flag telling sendfile to wait")
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement