473,471 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

zipped socket



Is there anyway open a socket so that every send/listen/recv
goes thru a zipping/unzipping process automatically?

Thanks,
--j

Aug 8 '05 #1
3 2147
As far as I know, there is not a prefabbed solution for this problem. One
issue that you must solve is the issue of buffering (when must some data you've
written to the compressor really go out to the other side) and the issue of
what to do when a read() or recv() reads gzipped bytes but these don't produce any
additional unzipped bytes---this is a problem because normally a read() that
returns '' indicates end-of-file.

If you only work with whole files at a time, then one easy thing to do is use
the 'zlib' encoding:
"abc".encode("zlib") "x\x9cKLJ\x06\x00\x02M\x01'" _.decode("zlib")

'abc'
... but because zlib isn't self-delimiting, this won't work if you want to
write() multiple times, or if you want to read() less than the full file

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC90dSJd01MZaTXX0RAlxtAKCYInTY85Hkbw1HUxGRAc IVQgnAPACgkQ8D
Qm3hbkJFwW7BZ1J34zd/4eE=
=E6gg
-----END PGP SIGNATURE-----

Aug 8 '05 #2
John wrote:

Is there anyway open a socket so that every send/listen/recv
goes thru a zipping/unzipping process automatically?


You ought to be able to do this easily by wrapping a bz2 compressor
around the socket (maybe using socket.makefile() to return a file object
first) and probably using a generator as well:

http://effbot.org/librarybook/bz2.htm includes relevant examples (not
specifically with sockets though).

Googling for "python incremental compression" ought to turn up any other
alternatives.

-Peter
Aug 8 '05 #3
je****@unpythonic.net wrote:
As far as I know, there is not a prefabbed solution for this problem. One issue that you must solve is the issue of buffering (when must some data you've written to the compressor really go out to the other side) and the issue of what to do when a read() or recv() reads gzipped bytes but these don't produce any additional unzipped bytes---this is a problem because normally a read() that returns '' indicates end-of-file.

If you only work with whole files at a time, then one easy thing to do is use the 'zlib' encoding:
>>> "abc".encode("zlib") "x\x9cKLJ\x06\x00\x02M\x01'" >>> _.decode("zlib")
'abc'
... but because zlib isn't self-delimiting, this won't work if you

want to write() multiple times, or if you want to read() less than the full file


That's basically a solved problem; zlib does have a kind of
self-delimiting. The key is the 'flush' method of the
compression object:

some_send_function( compressor.flush(Z_SYNC_FLUSH) )

The Python module doc is unclear/wrong on this, but zlib.h
explains:

If the parameter flush is set to Z_SYNC_FLUSH, all pending
output is flushed to the output buffer and the output is
aligned on a byte boundary, so that the decompressor can get
all input data available so far.
There's also Z_FULL_FLUSH, which also re-sets the compression
dictionary. For a stream socket, we'd usually want to keep the
dictionary, since that's what gives us the compression. The
Python doc states:

Z_SYNC_FLUSH and Z_FULL_FLUSH allow compressing further
strings of data and are used to allow partial error recovery
on decompression

That's not correct. Z_FULL_FLUSH allows recovery after errors,
but Z_SYNC_FLUSH is just to allow pushing all the compressor's
input to the decompressor's output.
--
--Bryan
Aug 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
0
by: Craig D | last post by:
I got really tired of unpacking a distutils package, installing then cleaning up. So I wrote distinstall.py - it reads a zip file, unpacks it, installs the bits and cleans up. I've only used it on...
2
by: Peter yeshew | last post by:
Create a database in a zipped format I have a function that creates a database. Is there any way to make the function create the database in a zipped form?TThe function i have is: ...
10
by: Yogi_Bear_79 | last post by:
pardon my ignorance as I am a self-taught hobbyist programmer. I am curious after reading up on SharpZipLib. Can I embed a zipped txt file in my program? Then either read from within the zip...
1
by: johnnyh | last post by:
I am currently using ASP.Net / C# to partially upload the first 100K of a CSV file to programattically extract the header information for field mapping against our database and for a test sample of...
0
by: Amelyan | last post by:
I have a bunch of files I need to put into Compressed (zipped) Folder, MyFiles.zip file, similarly to what Windows XP does when you send files to compressed zipped folder, so users can download...
9
by: peter.bremer | last post by:
Hi all, I've got a SQL Server database that contains zipped information stored in (binary) image fields. To complicate things, this zipped data is combined with plain-text data. I've verified...
1
by: 7seven7 | last post by:
Once i zipped my file a simple .xls spread sheet i write it to a database ... evoking the toArray() method of the memory stream, however now trying to recover this zipped file it doesn't display the...
3
by: s3raph | last post by:
Hi all, I have just inherited some code that sends a file from server to client. However, the code only works for text files, and I'm trying to modify it so that it can send and receive zipped...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.