Python Program for Socket Networking

socket class

socket class is used to create low level networking interface. It is using some operating system socket APIs, so some behaviors may be platform dependent.

Socket families

  • AF_UNIX socket - Address bound to a file system node and it's represented as a string(uses UTF-8 encoding).
  • AF_INET - Address uses a pair (host, port), here host is a string represents either a host name in Internet domain example 'google.com' or an IPv4 address example '10.20.160.61' and port is an integer number.
  • AF_INET6 - Address is a tuple (host, port, flow_info, scope_id), here host and port is like same as mentioned above point, flow_info and scope_id are optional, these denote sin6_flowinfo and sin6_scope_id members in struct sockaddr_in6 in C.
  • AF_NETLINK - Address is a pair (pid, groups).
  • AF_TIPC - TIPC is an open, non-IP based networked protocol
  • AF_BLUETOOTH - supports protocols like BTPROTO_L2CAP, BTPROTO_RFCOMM, BTPROTO_HCI and BTPROTO_SCO.
  • AF_ALG - Linux-only socket based interface to Kernel cryptography.

socket module

Some socket constants

Some constants are related to mention address family.
  • socket.AF_UNIX
  • socket.AF_INET
  • socket.AF_INET6
These constants are related to socket types.
  • socket.SOCK_STREAM
  • socket.SOCK_DGRAM
  • socket.SOCK_RAW
  • socket.SOCK_RDM
  • socket.SOCK_SEQPACKET

Socket Exceptions

  • socket.error - This class was made an alias of OSError.
  • socket.herror - subclass of OSError exception and raised for address-related errors when calling gethostbyname_ex() and gethostbyaddr() functions
  • socket.gaierror - subclass of OSError exception and raised for address-related errors when calling getaddrinfo() and getnameinfo()
  • socket.timeout - subclass of OSError exception and raised when a timeout occurs on a socket settimeout()

Some functions

  • soket.socket - creating socket object.
    socket.socket(family=AF_INET, type=SOCK_STREAM, protocol_number=0, file_number=None)
    
  • socket.socketpair - creating a pair of connected socket objects.
    socket.socketpair([family[, type[, protocol_number]]])
    
  • socket.create_connection - Connecting TCP service listening on the address (tuple (host, port)) and return the socket object.
    socket.create_connection(address[, timeout[, source_address]])
    
  • socket.getaddrinfo - function returns a list of tuples (family, type, protocol number, canonical name, socket address)
    socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)
    
  • socket.gethostbyname - gets IPv4 address as a string from host name.
    socket.gethostbyname(hostname)
    
  • socket.gethostname - gets the hostname of the local machine.
    socket.gethostname()
    
  • socket.gethostbyaddr - get the host name (host_name, alias_name_list, ip_address_list) for given ip address where hostname is the primary host name.
    socket.gethostbyaddr(ip_address)
    

Python program to get ip address of internet domain name.

import socket                                                                   
                                                                                
port = 80                                                                       
try:                                                                            
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                       
    print "Socket object is created"                                            
    host_ip = socket.gethostbyname('www.codingpointer.com')                     
except (socket.error, socket.gaierror) as ex:                                   
    print "socket error: %s" %(ex)                                              
else:                                                                           
    # connecting server                                                         
    s.connect((host_ip, port))                                                  
    print "Connected codingpointer.com on port == %s" %(host_ip)  

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^