Simple file transfer with python
ftrans is simple file transfer system for using promptly. Every linux computer is intalled. It makes easy to use this file transfer script.
python -m compileall
#----------------------------------------------
# ftrans.py
#----------------------------------------------
import sys
import socket # Import socket module
#----------------------------------------------
# Global variables
#----------------------------------------------
global gip, gport, gfile
gip = '127.0.0.1'
gport = 60000
gfile = "aa.tar"
#----------------------------------------------
# Functions
#----------------------------------------------
def usage():
print("Usage: " + sys.argv[0] + " s <port>")
print(" " + sys.argv[0] + " c <ip> <port> <filename>")
def run_server():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
host = '0.0.0.0' # Any cf.socket.gethostname()
s.bind((host, gport)) # Bind to the port
s.listen(5) # Now wait for client connection.
print('Server listening... (Port:' + str(gport) + ")")
while True:
conn, addr = s.accept() # Establish connection with client.
print('Connection from : ', addr)
data = conn.recv(1024)
print('Server received : ', repr(data))
#gfile = 'aa.tar'
gfile = str(data)
file = open(gfile,'rb')
buf = file.read(1024)
while (buf):
conn.send(buf)
#print('Sent ',len(buf))
buf = file.read(1024)
file.close()
print('Sending Completed')
conn.send('Thank you for connecting')
conn.close()
def run_client():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
print("connecting to " + gip)
s.connect((gip, gport))
s.send(gfile)
cnt = 0
with open(gfile, 'wb') as file:
print 'file opened'
while True:
#print('receiving data...')
data = s.recv(1024)
#print('data=', cnt, len(data))
if not data:
break
# write data to a file
file.write(data)
file.close()
print('Successfully get the file')
s.close()
print('connection closed')
def send_udp(ip, port, file):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP socket
fp = open(file,'rb')
buf = fp.read(1024)
buflen = 0
print('Start...{0}:{1} {2}'.format(ip, port, file))
while (buf):
buflen = buflen + len(buf)
sock.sendto(buf, (ip, port))
buf = fp.read(1024)
fp.close()
sock.close()
print('Send completed : ' + str(buflen))
#----------------------------------------------
# Check & Run
#----------------------------------------------
argc = len(sys.argv)
print("Starting...(Argc:" + str(argc) + ")")
if argc > 1:
if sys.argv[1][0] == 's':
print("server:" + sys.argv[1])
if argc > 2:
gport = int(sys.argv[2])
run_server()
else:
usage()
sys.exit()
elif sys.argv[1][0] == 'c':
print("client:" + sys.argv[1])
if argc > 3:
gip = sys.argv[2]
gport = int(sys.argv[3])
if argc > 4:
gfile = sys.argv[4]
run_client()
else:
usage()
sys.exit()
else:
print("send udp:" + sys.argv[1])
if argc > 3:
gip = sys.argv[2]
gport = int(sys.argv[3])
if argc > 4:
gfile = sys.argv[4]
send_udp(gip, gport, gfile)
else:
usage()
sys.exit()
else:
usage()
sys.exit()
Leave a comment