1

I want to use socket.create_connection() to set a source address in a ping implementation in python.

But how can I then set the type and the protocol? Because, before, I did:

icmp = socket.getprotobyname("icmp") 
my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) 

But now, I do:

src_addr = socket.gethostbyname(src_addr) 
dest_addr = socket.gethostbyname(dest_addr) 
my_socket = socket.create_connection(dest_addr, socket.getdefaulttimeout(), src_addr) 

Is there something like my_socket.setproto()? I haven't found such a function in the documentation.

Thank you, Guillaume

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
gcomte
  • 51
  • 1
  • 7

1 Answers1

0

create_connection will create a TCP connection, so it is not going to do what you want. You need to use raw sockets as in the example you gave yourself. Why do you want to switch to use socket.create_connection?

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • First, thank you for your help! I want to set a source address and the only way I found is to use create_connection. I want to do the equivalent of ping -S x.x.x.x y.y.y.y – gcomte Aug 20 '12 at 10:51
  • Sorry, rather late answer. Have a look at this solution. I think this does the exact thing you tried to do. http://stackoverflow.com/questions/1117958/how-do-i-use-raw-socket-in-python – Hans Then Sep 10 '12 at 00:03