socket script from perl -> python

K

kettle

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()
 
B

Bjoern Schliessmann

kettle said:
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
 
J

Jerry Hill

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.
 
K

kettle

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
 
K

kettle

kettle said:
# 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-struct.htmlfor details.

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

Sion Arrowsmith

Hrvoje Niksic said:
kettle said:
# 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-struct.html 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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top