---------------------------
Python Networking:
---------------------------
-Sharing data between a Connected network(Wired/Wireless)
Python Network Services:
---------------------------------
There are two levels of network service access in Python. These are:
1. Low-Level Access(connect same system(client and server) via socket)
2. High-Level Access(Application level network protocols(HTTP, FTP.,,)
Socket:
--------
-A socket is the end-point in a flow of communication between two programs or communication channels operating over a network.
Syntax:
--------
g = socket.socket (socket_family, type_of_socket, protocol=value)
Some of the important server socket methods are:
------------------------------------------------------------
1) listen(): is used to establish and start TCP listener.
2) bind(): is used to bind address (host-name, port number) to the socket.
3) accept(): is used to TCP client connection until the connection arrives.
4) connect(): is used to initiate TCP server connection.
5) send(): is used to send TCP messages.
6) recv(): is used to receive TCP messages.
7) sendto(): is used to send UDP messages
8) close(): is used to close a socket.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Step by step Socket programming
---------------------------------------
Step1 : importing the socket library
Ex:
import socket
Step 2 : making a simple socket(Socket creation)
Ex:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here,
1) AF_INET - address family ipv4(internet protocol version 4)
2) SOCK_STREAM - Connection oriented TCP Protocol
Step 3: Connecting to a server (find the ip of the server)
Ex:
import socket
ip = socket.gethostbyname('www.google.com')
print(ip)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1: find the ip of the server using Python
import socket
try:
ip1 = socket.gethostbyname('www.google.com')
ip2 = socket.gethostbyname('www.gmail.com')
ip3 = socket.gethostbyname('www.facebook.com')
print(ip1)
print(ip2)
print(ip3)
except Exception as e1:
print(e1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 2: find the Machine host name using Python
import socket
try:
ip1 = socket.gethostbyname(socket.gethostname())
print(ip1)
except Exception as e1:
print(e1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 3 : An example script to connect to Google using socket programming in Python
import socket # for socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket successfully created")
except Exception as err:
print ("socket creation failed with error : ",err)
# default port for socket
port = 80
try:
host_ip = socket.gethostbyname('www.google.com')
except Exception as err1:
print ("there was an error resolving the host",err1)
sys.exit()
# connecting to the server
s.connect((host_ip, port))
print ("the socket has successfully connected to google on port =",host_ip)
Output:
---------
Socket successfully created
the socket has successfully connected to google on port == 216.58.200.132
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 4 : Client and server Program
Step1: (save client1.py)
-----
# 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
ip1 = socket.gethostbyname(socket.gethostname())
s.connect((ip1, port))
# receive data from the server
print(s.recv(1024))
# close the connection
s.close()
----------------------------
Step2: (server1.py)
# 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()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment