Connect Function Of Python Socket Class

Overview:

  • The connect() method of Python’s socket module, connects a TCP(Transmission Control Protocol) based client socket to a TCP based server socket.
  • The connect() function initiates a 3-way handshake with the server socket and establishes the connection.
  • The IP address of a server socket is passed as the parameter to the connect() method.
  • Before passing an IP address to the connect() method it is important to make sure, it is of the right type - an IPv4 address or IPv6 address.
  • The type of IP address to be used with a socket is specified during the creation of a socket while calling the socket() function through the parameter family. For an IPv4 address socket.AF_INET to be used and for IPv6 address socket.AF_INET6.
  • The connect() function makes the accept() to be called on the server socket it is connecting to.
  • Once a connection is established to the remote socket the send(), sendall() methods can be used for sending data to the server. In the similar way the recv() function can be used for receiving data from the server socket.
  • The recv()  function wait for a message to arrive in the socket unless the socket is non-blocking mode and read up to the specified buffer size.

Example:

# ----- Python program for querying whois information -----

import socket

 

# Create a TCP socket

# By default family=AF_INET, type=SOCK_STREAM

whoisClient = socket.socket(); 

 

# Make a connect to the server - TCP does a 3 way handshake

whoisClient.connect(("whois.avalidwhoisserver.tld", 43)); # Active open

whoisClient.sendall("example.com".encode());   

 

# Buffer size to receive reply from whois server

bufferSize = 4096;

 

# Read the whole reply from whois server

while(True):

    data = whoisClient.recv(4096);

    lines = data.decode().splitlines();

    if(len(lines) > 0):

        for line in lines:

            if(len(line)>0):

                print(line);

 

    if(data==b''):

        print("Connection closed");

        break;

 

Output:

Domain Name: EXAMPLE.COM

   Registry Domain ID: 2336799_DOMAIN_COM-VRSN

   Registrar WHOIS Server: whois.iana.org

   Registrar URL: http://res-dom.iana.org

   Updated Date: 2018-08-14T07:14:12Z

   Creation Date: 1995-08-14T04:00:00Z

   Registry Expiry Date: 2019-08-13T04:00:00Z

   Registrar: RESERVED-Internet Assigned Numbers Authority

   Registrar IANA ID: 376

   Registrar Abuse Contact Email:

   Registrar Abuse Contact Phone:

   Domain Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited

   Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited

   Domain Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited

   Name Server: A.IANA-SERVERS.NET

   Name Server: B.IANA-SERVERS.NET

   DNSSEC: signedDelegation

   DNSSEC DS Data: 31589 8 1 3490A6806D47F17A34C29E2CE80E8A999FFBE4BE

   DNSSEC DS Data: 31589 8 2 CDE0D742D6998AA554A92D890F8184C698CFAC8A26FA59875A990C03E576343C

   DNSSEC DS Data: 43547 8 1 B6225AB2CC613E0DCA7962BDC2342EA4F1B56083

   DNSSEC DS Data: 43547 8 2 615A64233543F66F44D68933625B17497C89A70E858ED76A2145997EDF96A918

   DNSSEC DS Data: 31406 8 1 189968811E6EBA862DD6C209F75623D8D9ED9142

   DNSSEC DS Data: 31406 8 2 F78CF3344F72137235098ECBBD08947C2C9001C7F6A085A17F518B5D8F6B916D

   URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/

 Last up

date of whois database: 2019-06-25T11:51:17Z

For more information on Whois status codes, please visit https://icann.org/epp

NOTICE: The expiration date displayed in this record is the date the

registrar's sponsorship of the domain name registration in the registry is

currently set to expire. This date does not necessarily reflect the expiration

date of the domain name registrant's agreement with the sponsoring

registrar.  Users may consult the sponsoring registrar's Whois database to

view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois

database through the use of electronic processes that are high-volume and

automated except as reasonably necessary to register domain names or

modify existing registrations; the Data in VeriSign Global Registry

Services' ("VeriSign") Whois database is provided by VeriSign for

information purposes only, and to assist persons in obtaining information

about or related to a domain name registration record. VeriSign does not

guarantee its accuracy. By submitting a Whois query, you agree to abide

by the following terms of use: You agree that you may use this Data only

for lawful purposes and that under no circumstances will you use this Data

to: (1) allow, enable, or otherwise support the transmission of mass

unsolicited, commercial advertising or solicitations via e-mail, telephone,

or facsimile; or (2) enable high volume, automated, electronic processes

that apply to VeriSign (or its computer systems). The compilation,

repackaging, dissemination or other use of this Data is expressly

prohibited without the prior written consent of VeriSign. You agree not to

use electronic processes that are automated and high-volume to access or

query the Whois database except as reasonably necessary to register

domain names or modify existing registrations. VeriSign reserves the right

to restrict your access to the Whois database in its sole discretion to ensure

operational stability.  VeriSign may restrict or terminate your access to the

Whois database for failure to abide by these terms of use. VeriSign

reserves the right to modify these terms at any time.

The Registry database contains ONLY .COM, .NET, .EDU domains and

Registrars.

====================================================

% IANA WHOIS server

% for more information on IANA, visit http://www.iana.org

% This query returned 1 object

domain:       EXAMPLE.COM

organisation: Internet Assigned Numbers Authority

created:      1992-01-01

source:       IANA

This information was obtained from a different whois server, so we cannot verify its authenticity.

Connection closed

 

 


Copyright 2024 © pythontic.com