-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
51 lines (38 loc) · 1.05 KB
/
client.py
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
42
43
44
45
46
47
48
49
50
51
#Socket client example in python
import socket #for sockets
import sys #for exit
try:
#creat an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ', Error message : ' + msg[1]
sys.exit();
print 'Socket Created'
host = 'www.google.com';
port = 80;
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)
#connect to remote server
s.connect((remote_ip, port))
print('Socket Connected to ' + host + ' on ip ' + remote_ip)
#Send some data to remote server
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 sent successfuly'
#Now recieve data
reply = s.recv(4096)
print reply
#close the socket
s.shutdown(1); s.close()