Python network programming examples

Python network programming examples Tutorial: The python programming language has libraries that provide higher-level access to specific application-level network protocols like FTP, HTTP, etc. The python programming will provide two levels of access to network services. Then we access the basic socket support in the operating system that allows you to implement clients and servers. Here server and client both connection-oriented and connectionless protocols.
Sockets:-
They are endpoints of a bidirectional communications channel.
Also communicate within a process between the same machine, and different continents.
The sockets may be implemented several different channel types as,
 UNIX domain sockets,
 TCP, UDP.
The socket library will provide a specific class for handling common transports as well as a generic interface.

Build a Simple Server:-

Methods are as follows,

  1. s = socket. socket
  2. s.bind (): it binds address to a socket.
  3. s.listen (): and sets up and starts TCP listener.
  4. s.accept (): also accepts TCP client connection.

Steps for creating socket:-

  1. We create a socket.
  2. Then bind the socket to a port.
  3. And start accepting connections on the socket

 

Sr.No.

Term & Description

1

Domain:-is the family of the protocol used as a transport mechanism.

2

Type:-is communication between two endpoints for connection-oriented protocols and SOCK_DGRAM for the connectionless protocols.

3

Protocol:-used to identify the variant of protocol within a domain and type.

4

Hostname:-the dotted-quad address or an IPv6 in the colon.
The string specifies the INADDR_BROADCAST address.

Socket Module:-

You need to use socket.socket () function to create the socket.
The syntax is:-
      s = socket. socket (socket_family, socket_type, protocol=0)
The parameters are,

  1. socket_family − It is either AF_UNIX or AF_INET.
  2. socket_type − SOCK_STREAM or SOCK_DGRAM.
  3. Protocol − this is left out, defaulting to 0.

Methods:-

The server socket methods are,

  1. listen (): they are used to establish and start TCP listener.
  2. bind (): used to bind-address to the socket.
  3. accept (): used to TCP client connection until the connection arrives.
  4. sendto(): used to send UDP messages
  5. close(): is used to close a socket.
  6. connect (): used to initiate TCP server connection.
  7. send (): used to send TCP messages.
  8. recv (): used to receive TCP messages.

A Simple Server:-

We will use the socket function to create a socket object used to call other functions to set up a socket server.
We have to call bind function to specify a port of service on the given host.
Then accept method of the returned object the method waits until a client connects to the port.
import socket
s = socket. socket ()    
host = socket.gethostname () 
port = 12345           
s.bind ((host, port))         
s.listen (5)         
while True:
c, addr=s.accept ()
Print’Got connection from’, addr
 c.send (‘Thank you for connecting’)
c.close ()        

A Simple Client:-

The socket.connect will open a TCP connection to the hostname on the port.
After opening socket, you can read it like any IO objects.
import socket
s=socket. socket ()
host=socket.gethostname ()
port=12345
s.connect ((host, port))
print s.recv (1024)
s.close ()
Run the server.py as,
$python server.py&
$python client.py
           Got connection from ('127.0.0.1', 48437)
                      Thank you for connecting

The internet modules:-

Protocol

Common function

Port No

Python module

HTTP

Web pages

80

httplib, urllib, xmlrpclib

SMTP

Sending email

25

smtplib

POP3

Fetching email

110

poplib

IMAP4

Fetching email

143

imaplib

Telnet

Command lines

23

telnetlib

Gopher

Document transfers

70

gopherlib, urllib

NNTP

Usenet news

119

nntplib

FTP

File transfers

20

ftplib, urllib

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India