Created
February 12, 2019 09:03
-
-
Save jamesbjackson/3356a3a1e4f216b4d7a9c9b1d9e96207 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import fcntl | |
import struct | |
import IN | |
import sys | |
interface_name = str(sys.argv[1]) | |
print interface_name | |
def get_ip_address(ifname): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl( | |
s.fileno(), | |
0x8915, | |
struct.pack('256s', ifname[:15]) | |
)[20:24]) | |
ip = get_ip_address(str(interface_name)) | |
print ip | |
server_address = ('www.python.org', 80) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, interface_name) | |
s.connect(server_address) | |
request_header = 'GET / HTTP/1.0\r\nHost: www.python.org\r\n\r\n' | |
s.send(request_header) | |
response = '' | |
while True: | |
recv = s.recv(1024) | |
if not recv: | |
break | |
response += recv | |
print response | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment