Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27574
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-08-21 10:00 -0700 |
| References | <cbafe578-0a1a-4d0b-82a5-54c9f84fcd67@googlegroups.com> <4edc5ccb-9121-47b0-8ad4-2bf106735532@googlegroups.com> <50323dc7$0$6841$e4fe514c@news2.news.xs4all.nl> <c8381655-3c9a-4100-81af-bcd3f07414f0@googlegroups.com> <mailman.3568.1345483663.4697.python-list@python.org> |
| Subject | Re: How to set the socket type and the protocol of a socket using create_connection? |
| From | Guillaume Comte <guillaume.comte10@gmail.com> |
| Message-ID | <mailman.3606.1345568436.4697.python-list@python.org> (permalink) |
Unfortunatly, my_socket.bind((src_addr, 1)) doesn't work. I get the error message: "socket.error: [Errno 49] Can't assign requested address"...
I've tried to change the protocol to IPPROTO_RAW. Here is a simple example of the code:
import socket
import os
import struct
import time
import select
ICMP_ECHO_REQUEST = 8
PACKET_SIZE = 64 # Bytes
TIMEOUT = 0.5 # Seconds
def do_one(src_addr, dest_addr):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
if src_addr != None:
src_addr = socket.gethostbyname(src_addr)
my_socket.bind((src_addr, 1))
my_id = os.getpid() & 0xFFFF
print "id: " + str(my_id)
print "sending..."
send_one(dest_addr, my_socket, my_id)
print "receiving..."
id = receive_one(my_socket)
if id == None:
print "nothing received !"
else:
print "received id: " + str(id)
my_socket.close()
def checksum(source_string):
...
def send_one(addr, my_socket, id):
# Header: type (8), code (8), checksum (16), id (16), sequence number (16)
cs = 0
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, cs, socket.htons(id), 0)
data = PACKET_SIZE * "G"
cs = checksum(header + data)
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(cs), socket.htons(id), 0)
packet = header + data
my_socket.sendto(packet, (socket.gethostbyname(addr), 1))
def receive_one(my_socket):
while True:
what_ready = select.select([my_socket], [], [], TIMEOUT)
if what_ready[0] == []:
return None
received_packet = my_socket.recvfrom(1024)[0]
header = received_packet[20:28]
id = struct.unpack("bbHHh", header)[3]
return socket.ntohs(id)
if __name__ == "__main__":
import sys
dst = sys.argv[1]
print "dst: " + dst
try:
src = sys.argv[2]
print "src: " + src
except IndexError:
src = None
do_one(src, dst)
But when I try to set a source address, I still get the same error message...
Does anyone know how I could build the IP header (I don't even know if it can be the solution but it's worth a try...)?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 02:14 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 05:36 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Hans Mulder <hansmu@xs4all.nl> - 2012-08-20 15:38 +0200
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-20 08:04 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Dave Angel <d@davea.name> - 2012-08-20 13:27 -0400
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-21 10:00 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-21 10:00 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-21 15:28 -0400
Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-21 22:10 -0400
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 00:29 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 00:29 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Hans Mulder <hansmu@xs4all.nl> - 2012-08-22 11:03 +0200
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 02:21 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Chris Angelico <rosuav@gmail.com> - 2012-08-21 01:38 +1000
Re: How to set the socket type and the protocol of a socket using create_connection? Guillaume Comte <guillaume.comte10@gmail.com> - 2012-08-22 01:43 -0700
Re: How to set the socket type and the protocol of a socket using create_connection? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-22 15:18 -0400
Re: How to set the socket type and the protocol of a socket using create_connection? Grant Edwards <invalid@invalid.invalid> - 2012-08-23 13:53 +0000
csiph-web