WEB DEVELOPER SITE
PHP7
 

Python - Network Programming

In this chapter of about Python - Network Programming.

Python have two levels of access to network services.

Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

Socket Introduction

Sockets is communicate within a process, between processes on the same machine, or between processes on different continents.


A socket is a link between two applications that can communicate with one another (either locally on a single machine or remotely between two machines in separate locations).

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on

Sr.No. Term & Description
1

Domain

The family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.

2

type

The type of communications between the two endpoints, mostly SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.

3

protocol

It is used to identify a variant of a protocol within a domain and type.

4

hostname

The identifier of a network interface −

  • A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation

  • A string "<broadcast>", which specifies an INADDR_BROADCAST address.

  • A zero-length string, which specifies INADDR_ANY, or

  • An Integer, interpreted as a binary address in host byte order.

5

port

Each server listens for clients calling on one or more ports.

A port may be a Fixnum port number, a string containing a port number, or the name of a service.

The socket Module

In order to create a socket, you use the socket.socket() function, and the syntax is as simple as:

import sockets= socket.socket (socket_family, socket_type, protocol=0)

Here is the description of the arguments:

  • socket_family: Represents the address (and protocol) family. It can be either AF_UNIX or AF_INET.
  • socket_type: Represents the socket type, and can be either SOCK_STREAM or SOCK_DGRAM.
  • protocol: This is an optional argument, and it usually defaults to 0.

Client Socket Methods

Sr.No. Method & Description
1

s.connect()

This method actively initiates TCP server connection.

Build a Simple Server

Let's we can look at a simple Python server. The following are the socket server methods available in Python. A socket object is then used to call other functions to setup a socket server. There are following
  • s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • s.bind(): Binds address (hostname, port number) to socket.
  • s.listen(): Sets up and starts TCP listener.
  • s.accept(): Accepts TCP client connection.
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Define host
host = 'localhost'

# define the communication port
port = 8080

# Bind the socket to the port
sock.bind((host, port))
# Listen for incoming connections
sock.listen(1)

# Wait for a connection
print 'waiting for a connection'
connection, client = sock.accept()

print client, 'connected'

# Receive the data in small chunks and retransmit it

data = connection.recv(16)
print 'received "%s"' % data
if data:

    connection.sendall(data)
else:
    print 'no data from', client


# Close the connection
connection.close()

Server SIDE Output

$ python server.py
waiting for a connection
('127.0.0.1', 47050) connected
received "message"

Client SIDE Output

$ python client.py
connecting
message
socket closed




Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.

welookups.com