-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsockets.py
More file actions
41 lines (28 loc) · 683 Bytes
/
sockets.py
File metadata and controls
41 lines (28 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print "Failed to create socket"
sys.exit()
print "Socket created"
host = 'www.google.com'
port = 80
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print "Could not resolve"
sys.exit()
print "IP address is " + remote_ip
s.connect((remote_ip, port))
print 'Socket Connected to ' + host + ' on ipaddress ' + remote_ip
message = "GET / HTTP/1.1\r\n\r\n"
try:
s.sendall(message)
except socket.error:
print "Send failed"
sys.exit()
print 'Message send successfully'
reply = s.recv(4096)
print reply
s.close()