[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

socket script from perl -> python

kettle

2/7/2008 2:39:00 PM

Hi I have a socket script, written in perl, which I use to send audio
data from one server to another. I would like to rewrite this in
python so as to replicate exactly the functionality of the perl
script, so as to incorporate this into a larger python program.
Unfortunately I still don't really have the hang of socket programming
in python. The relevant parts of the perl script are below:
$host = '127.0.0.1';
my $port = 3482;

my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $length = length($converted_audio);

# pack $length as a 32-bit network-independent long
my $len = pack('N', $length);

#print STDERR "LENGTH: $length\n";

SOCKET->autoflush();

print SOCKET "r";
print SOCKET $len;
print SOCKET "$converted_audio\n";

while(defined($line = <SOCKET>)) {
....do something here...
}

------------
I've used python's socket library to connect to the server, and
verified that the first piece of data'r' is read correctly, the
sticking point seems to be the $len variable. I've tried using
socket.htonl() and the other less likely variants, but nothing seem to
produce the desired result, which would be to have the server-side
message print the same 'length' as the length printed by the client.

The python I've tried looked like this:
from socket import *
host = '127.0.0.1'
port = 3482
addr = (host, port)
s = socket(AF_INET, SOCK_STREAM)
s.connect(addr)

f = open('/home/myuname/socket.wav','rb')
audio = ""
for line in f:
audio += line

leng = htonl(len(audio))
print leng
s.send('r')
s.send(leng)
s.send(audio)
s.send("\n")
s.flush()

----------------------
of course I'd also like to s.recv() the results from the server, but
first I need to properly calculate the length and send it as a network
independent long. Any tips on how to do this would be greatly
appreciated!

6 Answers

Bjoern Schliessmann

2/7/2008 3:09:00 PM

0

kettle wrote:

> Hi I have a socket script, written in perl, which I use to send
> audio data from one server to another. I would like to rewrite
> this in python so as to replicate exactly the functionality of the
> perl script, so as to incorporate this into a larger python
> program. Unfortunately I still don't really have the hang of
> socket programming in python.

Socket programming in Python is just like socket programming in C. I
suppose with Perl it's the same.

> # pack $length as a 32-bit network-independent long
> my $len = pack('N', $length);
> [...]
> I've used python's socket library to connect to the server, and
> verified that the first piece of data'r' is read correctly, the
> sticking point seems to be the $len variable. I've tried using
> socket.htonl() and the other less likely variants, but nothing
> seem to produce the desired result, which would be to have the
> server-side message print the same 'length' as the length printed
> by the client.

Try struct.calcsize.

Regards,


Björn

--
BOFH excuse #88:

Boss' kid fucked up the machine

Jerry Hill

2/7/2008 3:40:00 PM

0

On Feb 7, 2008 9:39 AM, kettle <Josef.Robert.Novak@gmail.com> wrote:
> f = open('/home/myuname/socket.wav','rb')
> audio = ""
> for line in f:
> audio += line

I don't know anything about socket programming in python, but this bit
doesn't seem right for working on a binary file. You should just read
all of the data into audio in one go, like this:

f = open('/home/myuname/socket.wav','rb')
audio = f.read()

There's no need to iterate over the lines in the file, since it's
quite likely that there really aren't any 'lines' in a binary file.

--
Jerry

kettle

2/7/2008 4:06:00 PM

0

On Feb 8, 12:08 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.com> wrote:
> kettle wrote:
> > Hi I have a socket script, written in perl, which I use to send
> > audio data from one server to another. I would like to rewrite
> > this in python so as to replicate exactly the functionality of the
> > perl script, so as to incorporate this into a larger python
> > program. Unfortunately I still don't really have the hang of
> > socket programming in python.
>
> Socket programming in Python is just like socket programming in C. I
> suppose with Perl it's the same.
True, but I'm not talking about the concepts, I'm talking about the
idioms, which in python I don't know.


>
> > # pack $length as a 32-bit network-independent long
> > my $len = pack('N', $length);
> > [...]
> > I've used python's socket library to connect to the server, and
> > verified that the first piece of data'r' is read correctly, the
> > sticking point seems to be the $len variable. I've tried using
> > socket.htonl() and the other less likely variants, but nothing
> > seem to produce the desired result, which would be to have the
> > server-side message print the same 'length' as the length printed
> > by the client.
>
> Try struct.calcsize.
Thanks for the suggestion, I hadn't tried that one.

-joe


>
> Regards,
>
> Björn
>
> --
> BOFH excuse #88:
>
> Boss' kid fucked up the machine

Hrvoje Niksic

2/7/2008 7:01:00 PM

0

kettle <Josef.Robert.Novak@gmail.com> writes:

> # pack $length as a 32-bit network-independent long
> my $len = pack('N', $length);
[...]
> the sticking point seems to be the $len variable.

Use len = struct.pack('!L', length) in Python. See
http://docs.python.org/lib/module-s... for details.

kettle

2/8/2008 12:48:00 AM

0

On Feb 8, 4:01 am, Hrvoje Niksic <hnik...@xemacs.org> wrote:
> kettle <Josef.Robert.No...@gmail.com> writes:
> > # pack $length as a 32-bit network-independent long
> > my $len = pack('N', $length);
> [...]
> > the sticking point seems to be the $len variable.
>
> Use len = struct.pack('!L', length) in Python. Seehttp://docs.python.org/lib/module-stru... details.

Thanks, that was exactly what I was missing. And thanks for the link
as well! -joe

Sion Arrowsmith

2/8/2008 11:59:00 AM

0

Hrvoje Niksic <hniksic@xemacs.org> wrote:
>kettle <Josef.Robert.Novak@gmail.com> writes:
>> # pack $length as a 32-bit network-independent long
>> my $len = pack('N', $length);
>[...]
>> the sticking point seems to be the $len variable.
>Use len = struct.pack('!L', length) in Python. See
>http://docs.python.org/lib/module-s... for details.

Don't use "len = ..." -- shadowing the builtin "len" is asking for
trouble. The OP actually avoided this pitfall in his attempt at a
Python solution, translating "$len" to "leng".

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org...
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump