def _upload_from_file(s,fp,tofile,username): #filesize = os.path.getsize(fp.name); #filesize = os.fstat(fp.fileno()).st_size; #fp.seek(0); curr_pos = fp.tell() fp.seek(0,2); # Seek to end of file end_pos = fp.tell(); fp.seek(curr_pos); filesize = end_pos - curr_pos; # Select current file on server: s._select_current_file(filename=tofile,username=username); header = BIN_FILE + netutils.htonll(filesize); s._send_string(header); while (filesize > 0): buf = fp.read(min(filesize,16384)); l = len(buf); filesize -= l; if (l > 0): s._send_string(buf); else: s.disconnect(); raise IOError("Could not read remaining " + str(filesize) + " bytes of " + fp.name + "."); s._wait_for_bin_ack(); # The server sends an acknowledgment packet
def _request_file(s,filename,username,intent=""): # Select current file on server: s._select_current_file(filename=filename,username=username,intent=intent); # Send a BIN_REQ packet to request a file transfer: packet = BIN_REQUEST + netutils.htonll(0); s._send_string(packet);
def _json_rpc_request(s,method,params,request_id): """ Factory function for JSON-RPC 2.0 rpc requests. Returns a string that can be directly sent over the network connection to the RE atlas. """ msg = dict(); msg["jsonrpc"] = "2.0"; msg["method"] = method; if (params): msg["params"] = params; msg["id"] = request_id; json_string = json.dumps(msg); # Prepend the json string with a header request_string = JSON_MSG + netutils.htonll(len(json_string)) + json_string; return request_string;
def upload_from_string(s,string,remote_file,username=""): """ upload_from_string(string,remote_file,username=""): Upload a file from a string to the REatlas server. Arguments: string: Contents of the file as a string remote_file: Name to give the file on the server. username: If give, upload to this users folder instead of your own. """ size = len(string); s._select_current_file(filename=tofile,username=username); header = BIN_FILE + netutils.htonll(size); s._send_string(header); s._send_string(string); s._wait_for_bin_ack(); # The server sends an acknowledgment packet
#!/usr/bin/python import reatlas_client import netutils atlas = reatlas_client.REatlas("localhost",65535); atlas.connect(); atlas.build_functions(); #print(atlas.login(username="******",password="******")); #print(atlas._get_available_methods()); #print(atlas._select_current_file(filename="test")) atlas._socket.sendall(reatlas_client.BIN_REQUEST + netutils.htonll(0)); packtype = atlas._socket.recv(1); l = netutils.ntohll(atlas._socket.recv(8)); print("Type: " + str(netutils.ntohb(packtype)) + " (" + packtype + ")"); print("Length: " + str(l)); print(atlas._socket.recv(l)); atlas.disconnect();
#!/usr/bin/python import reatlas_client import netutils atlas = reatlas_client.REatlas("localhost",65535); atlas.connect(); atlas.build_functions(); print(atlas.login(username="******",password="******")); print(atlas._get_available_methods()); print(atlas._select_current_file(filename="test")) message = "HESTEHEST ! ! ! ! ! ! ! !" atlas._socket.sendall(reatlas_client.BIN_FILE + netutils.htonll(len(message))); atlas._socket.sendall(message); packtype, closed = netutils.readall(atlas._socket,1); l,closed = netutils.readall(atlas._socket,8); l = netutils.ntohll(l); #if (len(packtype) == 1 and len(l) == 8): if True: print("Type: " + str(netutils.ntohb(packtype)) + " (" + packtype + ")"); print("Length: " + str(l)); if (l > 0): print(atlas._socket.recv(l));