Python Networking Programming

  • The standard library of python supports network protocols, encoding as well as decoding of data (information) and also other networking concepts then it is less complex to implement network codes in Python better than C++.
  • The Python gives two levels of access to network services. First is low level, you will get the fundamental socket support in the basic operating system (OS) that allows you to execute clients and servers for connection-oriented as well as connectionless protocols.
  • Another one is high level; python are having libraries that give higher-level access to particular application-level network protocols like FTP, HTTP, etc.
  • By using sockets in the code to connect to a remote machine or loopback address (127.0.01) is python network programming.
Python Networking
Example:
import socket
import sys

HOST = 'localhost'
PORT = 8888 #any port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#putting s.bind() in try because in case if the port is already being used it will through an exception
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,