I am just getting started with python. I wanted to make a folder transfer tool to get familiar with python sockets.
import socket, os, sys
print"[*]Enter the IP[*]"
ip = sys.stdin.readline().rstrip()
port = 9999
# A file sender first
def file_sender(location, client_socket):
f = open(location, 'r')
data = f.read()
print("[*]Sending %s[*]" % location)
client_socket.sendall(data)
print("[*]Sent %s[*]" % location)
f.close()
client_socket.send("STOP")
client_socket.recv(4096)
# A file reciver for the reciver
def file_reciver(sv, client):
f = open(sv, 'a')
data = client.recv(1024)
while data:
f.write(data)
data = client.recv(1024)
if "STOP" in data:
break
f.write(data[:-4])
client.send("recived")
f.close()
print("[*]Saved %s[*]" % sv)
# A folder sender
def folder_sender(location, client_socket):
os.chdir(location)
location = os.getcwd()
no = len(list(os.listdir(location)))
client_socket.send(str(no))
print"[*]Preparing to send %d objects[*]" % no
res = client_socket.recv(1024)
print res
for x in os.listdir(location):
client_socket.send(x)
print("[*]Sending %s[*]" % x)
client_socket.recv(1024)
# Call the server manager
server_manager(x, client_socket)
os.chdir(location)
print(location)
# A folder reciver
def folder_reciver(sv, client):
try:
os.mkdir(sv)
except:
print "[*]File %s already exists can't overwrite[*]" % sv
sys.exit(0)
os.chdir(sv)
location = os.getcwd()
print location
no = client.recv(1024)
print ("[*]Preparing to recive %s objects[*]" % no)
client.send("[*]Ready to recive[*]")
for x in range(int(no)):
sv = client.recv(4096)
print ("Reciving %s" % sv)
client.send("Recived new object")
# call the client manager
client_manager(sv, client)
os.chdir(location) # Go to the previous diectory
print("Saved to %s" % location)
# Server manager, just seperates files from directories
def server_manager(location, client_socket):
if os.path.isdir(location):
client_socket.send("DIR")
folder_sender(location, client_socket)
else:
client_socket.send("not dir")
file_sender(location, client_socket)
def client_manager(sv, client):
c = client.recv(4096)
print c
if c == "DIR":
folder_reciver(sv, client)
else:
file_reciver(sv, client)
# A sender
def sender():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind((ip, port))
except:
print "[*]Couldn't bind the requested IP[*]"
sys.exit(0)
server.listen(1)
print "[*]Listening to %s : %d[*]" % (ip,port)
def seer(client_socket):
re = client_socket.recv(1024)
print re
print "[*]The path of the file to send[*]"
location = sys.stdin.readline().rstrip()
server_manager(location, client_socket)
print("[*]sent all[*]")
client, addr = server.accept()
print "[*]Accepted connection form %s : %d[*]" % (addr[0], addr[1])
seer(client)
# A reciver
def reciver():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((ip, port))
except:
print "[*]Coudn't connect to the requested server [*]"
sys.exit(0)
client.send("[*]Client is ready to recive[*]")
print "[*]Where to save the file[*]"
v = sys.stdin.readline().rstrip()
try:
os.chdir(v)
except:
print "[*]No such path/location [*]"
sys.exit(0)
print "[*]Save as[*]"
b = sys.stdin.readline().rstrip()
sv = os.path.join(v, b)
client_manager(sv, client)
while True:
print "[*]Type s to send or r to recive[*]"
command = (sys.stdin.readline()).rstrip()
if command.lower() == "s":
sender()
elif command.lower() == "r":
reciver()
else:
print "[*]Invalid Argument: %s" % command