[python-win32] remote copy with compress/decompress

Jens B. Jorgensen jens.jorgensen at tallan.com
Tue Dec 14 00:22:36 CET 2004


Well, this is pretty easy to do. The server side would look like this

import socket, sys, pickle
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 7777))
s.listen(5)
while True :
    clientsock, clientaddr = s.accept()
    # read size of file
    file_size = pickle.load(clientsock.makefile('r'))
    # now we read through the file and write it out
    f = file('outputfile', 'w')
    i = 0
    while i < file_size :
       buf = clientsock.recv(1024)
       if not buf :
          print 'error: client closed socket prematurely'
          sys.exit(-1)
       f.write(buf)
       i += len(buf)
    clientsock.close()
    f.close()
    # now do whatever else with your file

and the client code analogously looks like this:

import socket, pickle, os
fname = 'filetosend'
st = os.stat(fname)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 7777))
sw = s.makefile('w')
pickle.dump(st.st_size, sw)
f = file(fname, 'rb')
i = 0
while i < st.st_size :
    buf = f.read(1024)
    sw.write(buf)
    i += len(buf)
sw.close()
s.close()
f.close()


The above is simply the socket code to handle this file transfer. The 
server listens on port 7777, a number I just arbitrarily chose. The 
server listens for connections in a loop. When it receives a connection 
is first reads the size of the file (encoded in pickle format) and then 
opens up 'outputfile' to write the contents into. The just closes file 
and socket when done. The client does its end of this, connecting to 
port 7777 on localhost (in this case since I was doing this on my own 
machine), opening the file to be sent, sending the file's size and then 
the contents of the file. Easy!

python at kareta.de wrote:

>Hello,
>
>I'm wondering if i try a good solution for the following problem:
>
>We have an old DOS Application running on a W2K-Server which uses normal
>DOS-print and a workstation(w2k) in an outlet connected to the server via VPN.
>The workstation uses remote desktop to run the software and prints to LPT 1,2,3
>on the server. If I redirect the servers LPT's to a lokal printer of the
>workstation, printing is to slow. So I need a solution for this.
>
>My solution is as follow:
>1: capture DOS-print to file =>solved
>2: compress printfile => solved
>
>3: open a socket, transfer file to desktop
>4: decompress file
>step 3 and 4 is to develop a socket with python
>
>5: print it on lokal printer => solved
>
>What do you think about it. I think of sockets because step 4 should be done
>automatically when the file arrives at the workstation. Otherwise I have to
>check a directory on the workstation for incomming files every 20 seconds.
>
>Sorry, for asking a question that is not pure python. But I'm still learning
>and to know that python is not good for that, helps me also out :-)
>
>regards,
>Jürgen
>
>
>
>
>-------------------------------------------------
>This mail sent through IMP: http://horde.org/imp/
>_______________________________________________
>Python-win32 mailing list
>Python-win32 at python.org
>http://mail.python.org/mailman/listinfo/python-win32
>  
>


-- 
Jens B. Jorgensen
jens.jorgensen at tallan.com

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"  



More information about the Python-win32 mailing list