0% found this document useful (0 votes)
293 views

An Example Script To Connect To Google Using Socket

This document provides an example Python script to connect to Google using a socket. It imports the socket module, creates a socket, resolves Google's IP address, connects to Google on port 80, and prints a message confirming the successful connection.

Uploaded by

godutai appa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
293 views

An Example Script To Connect To Google Using Socket

This document provides an example Python script to connect to Google using a socket. It imports the socket module, creates a socket, resolves Google's IP address, connects to Google on port 80, and prints a message confirming the successful connection.

Uploaded by

godutai appa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# An example script to connect to Google using socket

import socket
import sys

try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket successfully created"
except socket.error as err:
print "socket creation failed with error %s" %(err)

# default port for socket


port = 80

try:
host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:

# this means could not resolve the host


print "there was an error resolving the host"
sys.exit()

# connecting to the server


s.connect((host_ip, port))

print "the socket has successfully connected to google \


on port == %s" %(host_ip)

output:
Socket successfully created
the socket has successfully connected to google
on port == 173.194.40.19
Server :

# first of all import the socket library


import socket

# next create a socket object


s = socket.socket()
print "Socket successfully created"

# reserve a port on your computer in our


# case it is 12345 but it can be anything
port = 12345

# Next bind to the port


# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))
print "socket binded to %s" %(port)

# put the socket into listening mode


s.listen(5)
print "socket is listening"

# a forever loop until we interrupt it or


# an error occurs
while True:

# Establish connection with client.


c, addr = s.accept()
print 'Got connection from', addr

# send a thank you message to the client.


c.send('Thank you for connecting')

# Close the connection with the client


c.close()
client:

# start the server


$ python server.py

$ telnet localhost 12345

Output :
# in the server.py terminal you will see
# this output:
Socket successfully created
socket binded to 12345
socket is listening
Got connection from ('127.0.0.1', 52617)

# In the telnet terminal you will get this:


Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingConnection closed by foreign host.
# Import socket module
import socket

# Create a socket object


s = socket.socket()

# Define the port on which you want to connect


port = 12345

# connect to the server on local computer


s.connect(('127.0.0.1', port))

# receive data from the server


print s.recv(1024)
# close the connection
s.close()

# start the server:


$ python server.py
Socket successfully created
socket binded to 12345
socket is listening
Got connection from ('127.0.0.1', 52617)

# start the client:


$ python client.py
Thank you for connecting

You might also like