import socket ip = "172.16.80.66" status_code = {} status_code['0'] = "Good" status_code['1012'] = "Argument not supported" status_code['1022'] = "File does not exist!" def sendCommand(ip, command): #print("Opening socket") sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, 10001)) #print("Sending command") sock.send(command + "\n") #print("Receiving response") data = "" while 1: got = sock.recv(4096) if not got: break data += got response = data.split("\n", 1) header = response[0].split(" ") data = response[1] response_code = header[0] #print("Handheld responded with status code " + response_code + " (" + status_code[response_code] + ")") if response_code == "0": return (data, header, sock) else: return (None, header, sock) def sendData(sock, command): sock.send(command) data = "" while 1: got = sock.recv(4096) if not got: break data += got response = data.split("\n", 1) header = response[0].split(" ") return (None, header) def dir(path): response = sendCommand(ip, "dir " + path) sock = response[2] sock.close() if response[0] != None: dirs = response[0].split("\n\n") print("Content of " + path) print("Size" + "\t" + "Time" + "\t" + "Bits" + "\t\t" + "Name") for dir in dirs: if dir == "": break data = dir.split("\n") name = data[0] size = data[1].split("=")[1] time = data[2].split("=")[1] tyep = data[3].split("=")[1] bits = data[4].split("=")[1] if tyep == "0": print(size + "\t" + time + "\t" + bits + "\t\t" + name) else: print(size + "\t" + time + "\t" + bits + "\t\t" + "<"+ name + ">") else: print(path + " is not a directory or does not exists! (" + response[1][0] + ")") def get(path, fle): response = sendCommand(ip, "fget " + path) sock = response[2] sock.close() if response[0]: print("Received file of " + response[1][1] + " bytes long") FILE = open(fle, "wb") FILE.write(response[0]) FILE.close() print("Wrote data to " + fle) else: print("Failed to get file ! (" + response[1][0] + ")") #dir("/phx/documents/test/") def put(local, path): file = open(local, "r") content = file.read() file.close() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, 10001)) response = sock.send("fput " + path + " " + str(len(content)) + " 0\n") #sock = response[2] sendData(sock, content) sock.close() if response[1][0] == "0" and False: response = sendData(sock, content) if response[1][0] == "0": print("File send") else: print("Failed to send file (" + response[1][0] + ")") else: print("Error while starting transfer (" + response[1][0] + ")") sock.close() print ("Usage") print ("dir /pxh/path/") print ("get /pxh/path/ /localpath/") print ("put /localpath/ /pxh/path/") while True: inp = raw_input(">").split() if len(inp)>0: if inp[0] == "dir": path = "/" if len(inp) >= 2: path = inp[1] dir(path) elif inp[0] == "get": path = "/" fle = "blaap" if len(inp) >= 2: path = inp[1] if len(inp) >= 3: fle = inp[2] get(path, fle) elif inp[0] == "put": if len(inp) == 3: put(inp[1], inp[2])