So I can print them out and sticky tape them to my wall.
Please note: A fantastic tutorial I reference from is BinaryTides. Found here
I am breaking this tutorial up into my own notes, to jog my memory. So it is not a tutorial for others to follow, however if it is useful, its useful ! :)
Now "haydn" (aka me), python network programming requires a socket.
test
Now we need to get an IP address of a remote host/url
as such we connect to google
Note to self: Copy pasting looks way better than creating via Gist, saves time too!!
host
=
'www.google.com'
try
:
remote_ip
=
socket.gethostbyname( host )
except
socket.gaierror:
#could not resolve
print
'Hostname could not be resolved. Exiting'
sys.exit()
print
'Ip address of '
+
host
+
' is '
+
remote_ip
Now we have the IP address we can connect to it on a certain portsimply use s.connect
#Connect to remote server
s.connect((remote_ip , port))
print
'Socket Connected to '
+
host
+
' on ip '
+
remote_ip
But we need to add the port we wish to connect to
host
=
'www.google.com'
port
=
80
To Send data we do
sendall(message)
message
=
"GET / HTTP/1.1\r\n\r\n"
try
:
#Set the whole string
s.sendall(message)
except
socket.error:
#Send failed
print
'Send failed'
sys.exit()
print
'Message send successfully'
To receive data:
s.recv(4096)
s.recv(4096)
#Now receive data
reply
=
s.recv(
4096
)
print
reply
s.close()
No comments:
Post a Comment