-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsocket_connect_send.py
More file actions
43 lines (28 loc) · 984 Bytes
/
socket_connect_send.py
File metadata and controls
43 lines (28 loc) · 984 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
42
__author__ = 'rsukla'
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print "Socket is not created"
sys.exit(0)
print 'Socket Created'
host = 'www.google.com'
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror, msg:
print " Unable to resolve the error"
sys.exit(0)
# Abhi humko remote ip mil gaya hain , now we want to connect to that ip through a port number
s.connect((remote_ip, 80))
print "Socket connected to", host, "on remote ip", remote_ip
message = "GET / HTTP/1.1\r\n\r\n" # this is actually an HTTP command to fetch the main page
try:
s.sendall(message) #sendall is basically used to send data to the remote server
except socket.error, msg:
print "Message can't be sent"
sys.exit(0)
print "Message sent successfully "
reply = s.recv(4096)
print reply # Jis socket is connect kiya hain ..use socket se hain toh wapas receive karega
s.close()