Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.They are the real backbones behind web browsing. In simpler terms there is a server and a client.
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means connection oriented TCP protocol.
Now we can connect to a server using this socket.
Connecting to a server:
Note that if any error occurs during the creation of a socket then a socket.error is thrown and we can only connect to a server by knowing it’s ip. You can find the ip of the server by using this :
Note that if any error occurs during the creation of a socket then a socket.error is thrown and we can only connect to a server by knowing it’s ip. You can find the ip of the server by using this :
$ ping www.google.comimport socket ip = socket.gethostbyname('www.google.com') print ip#Example of a SCRIPT# An example script to connect to Google using socket# programming in Pythonimportsocket# for socketimportsystry:s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)"Socket successfully created"exceptsocket.error as err:"socket creation failed with error %s"%(err)# default port for socketport=80try:host_ip=socket.gethostbyname('www.google.com')exceptsocket.gaierror:# this means could not resolve the host"there was an error resolving the host"sys.exit()# connecting to the servers.connect((host_ip, port))"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
- First of all we made a socket.
- Then we resolved google’s ip and lastly we connected to google.
- Now we need to know how can we send some data through a socket.
- For sending data the socket library has a sendall function. This function allows you to send data to a server to which the socket is connected and server can also send data to the client using this function.
A simple server-client program :
Server :
A server has a bind() method which binds it to a specific ip and port so that it can listen to incoming requests on that ip and port.A server has a listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And last a server has an accept() and close() method. The accept method initiates a connection with the client and the close method closes the connection with the client.
# first of all import the socket libraryimport socket # next create a socket objects = socket.socket() print "Socket successfully created"# reserve a port on your computer in our# case it is 12345 but it can be anythingport = 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 networks.bind(('', port)) print "socket binded to %s" %(port)# put the socket into listening modes.listen(5) print "socket is listening" # a forever loop until we interrupt it or # an error occurswhile 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()
|
No comments:
Post a Comment