Recent Posts

A journey to enter socket module in python 3x (Part 1)

Hello everyone. Today i am introducing you a simple server make with python 3.4.3(actually i use 3.4.2 because in my kali linux it was default and more over that i didn't do it by windows because windows is sucks for networking).

Now first the basic.


What is socket ?
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
source. But in my word socket is a low level networking level phase.

 The main basic thing for socket programming is
For server :
  1. Make a socket object.
  2. Bind the port.
  3. Create listen with number of backlog(how many connection can take at a time,default it force 0).
  4. Accept current connection.
  5. Close connection (most important you can notice why i say it)

Now here is the main difference between python2 and python3 socket module.

Python 2 socket module : In this module the data flow can be done with string. So when you get a data you can print it by directly (i am not sure) or make it string with str(data) then print.

Python 3 socket module : In this module the data flow can't be done with string. Remember - when you got a data in python 3, that must be a decoded data, so whenever you send a data encode it and send, whenever you got a data then decode it and read.

Now here i start a simple server.py


import socket,sys
host = "127.0.0.1"
port = 5000
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((host,port))
sock.listen(5)

current_address , info = sock.accept()
print('connected with ',info[0],info[1])

while True:
    try:
        data = current_address.recv(1024).decode('utf-8')
        print('Durdin says : ',data)
        if not data:break
        data = input('Devils -> : ')
        current_address.sendall(data.encode('utf-8'))

    except Exception as e:
        print(e)
        sys.exit()
        sock.close()

sock.close()


 Here all functions mean :
  • socket.socket(): Create a new socket using the given address family, socket type and protocol number. [use for create a socket object]. AF_INET mean your connection could be ipv4 connection which consist with only ip and port. Its a socket family type. SOCK_STREAM is socket.bind(address): Bind the socket to address.
  • socket.listen(backlog): Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 0.Better if you give it 5.
  • socket.accept(): The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
  • socket.send(Here your encode message): Send data to the socket. The socket must be connected to a remote socket. Returns the number of bytes sent.
  • socket.close(): Mark the socket closed. all future operations on the socket object will fail. If you don't then the listen mode is on and you can't connection if your program fail. Then you have to kill the program.How ?? Here is the resource http://stackoverflow.com/questions/29217502/socket-error-address-already-in-use

We completely create server.py
Next article Here

Post a Comment

0 Comments