forked from Qianfinland/TCP_Socket_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectClient.py
More file actions
executable file
·39 lines (31 loc) · 813 Bytes
/
connectClient.py
File metadata and controls
executable file
·39 lines (31 loc) · 813 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
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: '+ str(msg[0]) + ', Erros message: ' + msg[1]
sys.exit();
print 'Socket Created'
port = 80
host = 'www.google.com'
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
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 data to remote server
message = "GET / HTTP/1.1\r\n\r\n"
try :
s.sendall(message)
except socket.error:
print 'Fail to send'
sys.exit()
print 'Sending message successfully'
#receive data
reply = s.recv(4096)
print reply
s.close()