sockets - Is it possible to transfer a text file from server to client using python? -
i newbie in python , have written code server-client in python. code sends file o/p server client console. code:
from thread import * import threading import time import psutil import itertools import ctypes import string import os import sys import socket exitflag = 0 #function returning list of valid partitions of system... def drives(): drive_bitmask = ctypes.cdll.kernel32.getlogicaldrives() return list(itertools.compress(string.ascii_uppercase,map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1]))) def proc_info(conn,addr): # prints process's info match keyword....... server side f0=open('abc.txt','a+') conn.send('welcome server\n') name=[] c=drives() k=0 conn.send("enter key...\n") # authentication purposes...e.g.here key 1 t=conn.recv(8) if(t!='1'): #wrong key....invalid user conn.send("\ninvalid key..teminating connection.....abort\n") conn.close() else: r="" conn.send("\nenter process keyword ..press # @ end ") # e.g. 'sk' skype d=conn.recv(65536).decode('utf-8') while(d!='#'): r=r+str(d) d=conn.recv(65536).decode('utf-8') ## problematic statement f0.write(d) p in psutil.pids(): # iterates through pids of processes obtained try: p1=psutil.process(p) if(r in p1.name()): p2=p1.get_memory_info() t=p1.name()+' ' d=str(p)+' ' f0.write(d) f0.write(t) conn.send(d)# prints pid of process conn.send(t),# prints name of process d=str(p2[0]/(1024*1024))+' ' conn.send(d) # print memory in mb f0.write(d) f0.write('mb\n') conn.send('mb\n') connect in p1.connections(kind='tcp'): d=str(connect.laddr[0])+' ' f0.write(d) conn.send(d) # prints ip d=str(connect.laddr[1])+' ' f0.write(d) conn.send(d) # prints tcp ports f0.write('\n') conn.send('\n') if((p2[0]/(1024*1024))>=100): m=p1.name() l in c: f=0 root, dirs, files in os.walk(l): # walks through entire file system of windows os #f=0 name in files: if name==m: #p1.kill() f=1 #os.system(os.path.abspath(os.path.join(root, name))) #subprocess.popen(os.path.abspath(os.path.join(root,name))) # relaunches killed process #if(p): break if(f==1): break if(f==1): break #l=l+1 except psutil.accessdenied: pass except psutil.nosuchprocess: pass else: continue f0.close() conn.close() class serverthread(threading.thread): # thread class server(for listening) def __init__(self, threadid, name,): threading.thread.__init__(self) self.threadid = threadid self.name = name def run(self): threadlock.acquire() host=raw_input("enter hostname.....") host=socket.gethostbyname(host) port=60000 # specific port available s=socket.socket(socket.af_inet, socket.sock_stream) print 'socket created' #bind socket local host , port try: s.bind((host, port)) except socket.error msg: print 'bind failed. error code : ' + str(msg[0]) + ' message ' + msg[1] sys.exit() print 'socket bind complete' s.listen(10) # no of connections @ 1 time print self.name+' listening' threadlock.release() while 1: conn, addr = s.accept() # connection gets established here.. print 'connected ' + addr[0] + ':' + str(addr[1]) conn.send('thank connecting') # creates new thread client services , processes parameter , sends client start_new_thread(proc_info,(conn,addr)) s.close() threadlock=threading.lock() thread1 =serverthread(1,"server 1") thread1.start() # starting server thread...
this code,at present,prints output client console.is possible send entire text file client? e.g., if have "xyz.txt" on server, can copy text file of "xyz.txt" created on client?
if want transfer file client side, there no need build wheel, use this:
python -m simplehttpserver 8081
this build simple file server , client either chose download file browser or wget
.
and if cant have control of client side(that wont run code written you) it's highly impossible save file on disk.
Comments
Post a Comment