icmp and raw sockets in python

2011-12-13 Thread Sagy Drucker
hello
i am relatively new to python, so please be considerate...

i'm implementing a server and a client via raw_sockets.
i have the necessary privileges.

now, the server i defined so:
host = socket.gethostbyname(socket.gethostname())
address = (host, 4)
sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_ICMP)
sockSer.bind(address)
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
packet, addr = sockSer .recvfrom(4096)   # wait for packet from client

Q1) why can't i simply type: hosts = 'localhost'.
if i do so, it doesn't allow me to write the line:
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON).
only when doing gethostbyname(socket.gethostname()) i get
192.168.1.101
and then it works.

in a different class:
the client socket:
host = socket.gethostbyname(socket.gethostname())
address = (host, 4)
sockCli = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_ICMP)

Q2) do i also need to type: sockCli.ioctl(socket.SIO_RCVALL,
socket.RCVALL_ON)
or maybe sockCli.connect(address)
for the client socket?

now, the problems arise when i do the following:
1) send a packet from client to socket:
header=...
payload='a'
sockCli.sendto(header + payload, address)

2) receive packet in server and send file to client:
while(true):
data, addr = sockSer.recvfrom(4096)
header2=...
payload2='b'
sockSer.sendto(header2 + payload2, addr)

now, my important question is:
Q3) the server sent only 1 packet to client, with payload 'b'.
what happens is, my client actually receives 2 packets in the while
loop:
first packet is what the client itself sent to server, and the other
packet is from the client got from the server.
hence my output is 'ab' instead of simply 'b'
why is this happening???

NOTE: i didn't type the entire code, but i think my
syntax,parsing,header composition etc.. are correct.
is there an obvious problem in my code?
if necessary i'll upload the entire code.

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: icmp and raw sockets in python

2011-12-13 Thread Martin P. Hellwig

On 13/12/2011 16:50, Sagy Drucker wrote:

hello

Hi

i am relatively new to python, so please be considerate...
As I am only responding to one of your questions, perhaps it would be 
best if you don't get any other more helpful replies to split your 
questions up and post them separately.


i'm implementing a server and a client via raw_sockets.
i have the necessary privileges.

now, the server i defined so:
host = socket.gethostbyname(socket.gethostname())
address = (host, 4)
sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_ICMP)
sockSer.bind(address)
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
packet, addr = sockSer .recvfrom(4096)   # wait for packet from client

Q1) why can't i simply type: hosts = 'localhost'.
if i do so, it doesn't allow me to write the line:
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON).
only when doing gethostbyname(socket.gethostname()) i get
192.168.1.101
and then it works.


cut other questions
Well localhost should resolve to 127.0.0.1/8 which is attached to the 
loopback interface, my gut feeling is that this interface has a 
particular set of restrictions which you are encountering.

Sorry I can't be more helpful.

--
mph
--
http://mail.python.org/mailman/listinfo/python-list


Re: udp sockets with python

2010-11-11 Thread Jean-Paul Calderone
On Nov 10, 9:23 pm, Tim Roberts t...@probo.com wrote:
 Mag Gam magaw...@gmail.com wrote:

 I am measuring the round trip time using tcpdump. The C version is
 giving me around 80 microseconds (average) and the python is giving me
 close to 300 microseconds (average).

 If you need the performance of a compiled language, then it's probably not
 appropriate to use an interpreter.

Ridiculous.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: udp sockets with python

2010-11-11 Thread r0g

On 11/11/10 16:03, Jean-Paul Calderone wrote:

On Nov 10, 9:23 pm, Tim Robertst...@probo.com  wrote:

Mag Gammagaw...@gmail.com  wrote:


I am measuring the round trip time using tcpdump. The C version is
giving me around 80 microseconds (average) and the python is giving me
close to 300 microseconds (average).


If you need the performance of a compiled language, then it's probably not
appropriate to use an interpreter.


Ridiculous.


Terse!
--
http://mail.python.org/mailman/listinfo/python-list


Re: udp sockets with python

2010-11-10 Thread Tim Roberts
Mag Gam magaw...@gmail.com wrote:

I am measuring the round trip time using tcpdump. The C version is
giving me around 80 microseconds (average) and the python is giving me
close to 300 microseconds (average).

If you need the performance of a compiled language, then it's probably not
appropriate to use an interpreter.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


udp sockets with python

2010-11-09 Thread Mag Gam
Hello,

When measuring round trip time for the UDP echo client/server the C
version is much faster. I was wondering if there is anything I can do
to speed up.

My current code for client looks like this

sock=socket(AF_INET,SOCK_DGRAM)
for x in range (1000):
  sock.sendto(foo,(server,port))
  a=sock.recv(256)

sock.close()


The server code looks like this:
UDPsock=socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
  m,c=UDPsock,recvfrom(1024)
  UDPsock.sendto('bar',c)

UDPsock.close()


I am measuring the round trip time using tcpdump. The C version is
giving me around 80 microseconds (average) and the python is giving me
close to 300 microseconds (average).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: udp sockets with python

2010-11-09 Thread Jean-Paul Calderone
On Nov 9, 5:20 am, Mag Gam magaw...@gmail.com wrote:
 Hello,

 When measuring round trip time for the UDP echo client/server the C
 version is much faster. I was wondering if there is anything I can do
 to speed up.

 My current code for client looks like this

 sock=socket(AF_INET,SOCK_DGRAM)
 for x in range (1000):
   sock.sendto(foo,(server,port))
   a=sock.recv(256)

 sock.close()

 The server code looks like this:
 UDPsock=socket(AF_INET,SOCK_DGRAM)
 UDPSock.bind(addr)
 while 1:
   m,c=UDPsock,recvfrom(1024)
   UDPsock.sendto('bar',c)

 UDPsock.close()

 I am measuring the round trip time using tcpdump. The C version is
 giving me around 80 microseconds (average) and the python is giving me
 close to 300 microseconds (average).

Try replacing the hostname in your send calls with an IP address.  If
you're not passing an IP address here, then the Python version has to
do a name lookup for each send, I bet your C version is not.

Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with sockets and python 2.5

2007-03-21 Thread Jose Alberto Reguero
El Lunes, 19 de Marzo de 2007, Jose Alberto Reguero escribió:
 I had two programs, server.py and client.py(attached)

 1:
   server.py at i386 python 2.4
   client.py at x86_64 python 2.5
 Work

 2:
   server.py at x86_64 python 2.5
   client.py at i386 python 2.4
 Don't work

 Any ideas?
 Thanks.

 Jose Alberto

The problem was with kernel 2.6.21-rc4.
I put kernel 2.6.20.3 and the programs work well.
Thanks.
Jose Alberto
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with sockets and python 2.5

2007-03-20 Thread Facundo Batista
Jose Alberto Reguero wrote:

 2:
   server.py at x86_64 python 2.5
   client.py at i386 python 2.4
 Don't work

What do you mean with don't work?

They crash? Your machine hungs? Your house explodes?

You'd be more specific in the error you get, and what behaviour you
expect.

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with sockets and python 2.5

2007-03-20 Thread Fabio FZero
On Mar 20, 10:05 am, Facundo Batista [EMAIL PROTECTED]
wrote:
 Jose Alberto Reguero wrote:
  2:
 server.py at x86_64 python 2.5
 client.py at i386 python 2.4
  Don't work

 What do you mean with don't work?

 They crash? Your machine hungs? Your house explodes?

Traceback (most recent call last):
  File server.py, line 1, in module
DontWorkError: YOUR HEAD A SPLODE!

:D

FZ

-- 
http://mail.python.org/mailman/listinfo/python-list


Problem with sockets and python 2.5

2007-03-19 Thread Jose Alberto Reguero
I had two programs, server.py and client.py(attached)

1:
server.py at i386 python 2.4
client.py at x86_64 python 2.5
Work

2:
server.py at x86_64 python 2.5
client.py at i386 python 2.4
Don't work

Any ideas?
Thanks.

Jose Alberto


clinet.py
Description: application/python


server.py
Description: application/python
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Problem with sockets and python 2.5

2007-03-19 Thread Steve Holden
Jose Alberto Reguero wrote:
 I had two programs, server.py and client.py(attached)
 
 1:
   server.py at i386 python 2.4
   client.py at x86_64 python 2.5
 Work
 
 2:
   server.py at x86_64 python 2.5
   client.py at i386 python 2.4
 Don't work
 
 Any ideas?

Fix them?

regards
  Steve
--
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings   http://holdenweb.blogspot.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets in python

2006-10-03 Thread OneMustFall


 What's your set-up and which cord are you pulling?


Well i now i think the clue is in the OS, i have sniffed and it seems
that Twisted have no magic.
It is seems that i simply tested things in a wrong way -
when i pulled cord from ethernet card windows determined that network
lost and started to closing all sockets opened to that network (and so
EINVAL or other OS error raised when twisted tryed to read wrom that
socket),
while the server did had a network - and it was right thing that server
was thinking that socket is alive.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets in python

2006-10-03 Thread Christophe
OneMustFall a écrit :
 
 What's your set-up and which cord are you pulling?

 
 Well i now i think the clue is in the OS, i have sniffed and it seems
 that Twisted have no magic.
 It is seems that i simply tested things in a wrong way -
 when i pulled cord from ethernet card windows determined that network
 lost and started to closing all sockets opened to that network (and so
 EINVAL or other OS error raised when twisted tryed to read wrom that
 socket),
 while the server did had a network - and it was right thing that server
 was thinking that socket is alive.

Stupid Windows behaviour btw! I hate that, it makes testing code 
stability for temporary network lags much much harder to do.

And don't get me started on when we reboot the switch which causes all 
open sockets on my computer to fail, even though the reboot only takes a 
few seconds and though the internet connection doesn't change.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets in python

2006-10-02 Thread Bryan Olson
OneMustFall wrote:
   Reciently i wrote a simple client (in twisted) using Reconnecting
 Factory.
   That client logins to my socket server.. and that`s it.
 
   Interesting thing is that it is seems that twisted client,
   sends some ping on a TCP level without sending any data to the
   socket directly.
   Because when i pull out cord from the ethernet card simulating
   network falure, client in about 10-15 seconds determines that
   connection lost!! (pretty cool)
   While my server thinks that client is connected.

What's your set-up and which cord are you pulling?


-- 
--Bryan
-- 
http://mail.python.org/mailman/listinfo/python-list


Sockets in python

2006-09-29 Thread OneMustFall

  Reciently i wrote a simple client (in twisted) using Reconnecting
Factory.
  That client logins to my socket server.. and that`s it.

  Interesting thing is that it is seems that twisted client,
  sends some ping on a TCP level without sending any data to the
  socket directly.
  Because when i pull out cord from the ethernet card simulating
  network falure, client in about 10-15 seconds determines that
  connection lost!! (pretty cool)
  While my server thinks that client is connected.
  I know that sockets ware designed to behave so.. to be tolarate
to
  network problems, but in my case this is bad.. i want it to
  behave like in twisted.

  I have searched in a socketmodule.c from python source code for a
  magic variables, but did not found anything that fits.

  I am using SO_KEEPALIVE on server - but documentations says
  it will only ping (and so timed outed) in a few hours.

  How do i make my server act the same way as twisted client?
  (to make some sort of a low level checking if a connections is
  broken without actualy sending anything to socket)

  The only suspicious lines intwisted is seems :

---
SO_UPDATE_ACCEPT_CONTEXT = 0x700B
SO_UPDATE_CONNECT_CONTEXT = 0x7010

--
try:
acc_sock.setsockopt(socket.SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT, struct.pack(I, handle))
except socket.error, se:
self.transport.acceptErr(ret, bytes)
else:
self.transport.acceptDone(acc_sock,
acc_sock.getpeername())

--
if have_connectex:
try:
sock.setsockopt(socket.SOL_SOCKET,
SO_UPDATE_CONNECT_CONTEXT, )
except socket.error, se:

self.transport.connectErr(failure.Failure(error.ConnectError()))
self.transport.connectDone()
---

I tried this all but it trows an exeption and not working and also
i have no i idea what
SO_UPDATE_ACCEPT_CONTEXT , andSO_UPDATE_CONNECT_CONTEXT  means.

Huh. Any Suggestions?

-- 
http://mail.python.org/mailman/listinfo/python-list