menu
AWESOME! NICE LOVED LOL FUNNY FAIL! OMG! EW!
How to Work with TCP Sockets in Python
A network/internet socket is anend-point of interposing communication across a computer network. In Python,the library has a module called socket which offers a low-level networkinginterface, it’s common across various programming languages since it usesOS-level system calls. When you create a socket, use a function called asocket.

How to Work with TCP Sockets in Python

A network/internet socket is anend-point of interposing communication across a computer network. In Python,the library has a module called socket which offers a low-level networkinginterface, it’s common across various programming languages since it usesOS-level system calls. When you create a socket, use a function called asocket. It accepts family, type, and prototype arguments. Creating a TCP-socket,for the family, you must use a socket.AF_INET or socket.AF_INET6 and for thetype you must use socket.SOCK_STREAM.

 

Example for Python socket

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

Main methods to create Python socket objects

·  bind() – specific for server sockets.

·  listen() – specific for server sockets.

·  accept() – specific for server sockets.

·  connect() – client sockets.

·  send() – both server and client sockets.

·  recv() – both server and client sockets.

For instance, echo server from documentation

import socket

s =socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((‘localhost’, 50000))

s.listen(1)

conn, addr = s.accept()

while 1:

    data =conn.recv(1024)

    if not data:

       break

    conn.sendall(data)

conn.close()

To create a server socket, bindit to localhost and 50000 port, and start listening for incoming connections.To accept an incoming connection call as accept(), this method will block untila new client connects. When it’s happening, will create a new socket andreturns it together with the client’s address. Then, an infinite loop readsdata from the socket in batches of 1 KB using method recv() till it returns ablank string. Next, it sends all incoming data back usinga method called sendall() which inside repeatedly calls send(). Next, to that,it just closes the client’s connection. 

A client-side code looks simply

Here instead of bind() andlisten() methods it calls only the connect() method and directly sends data tothe server. Then it receives 1 KB back, closes the socket then writes thereceived data. Each socket method is blocking. For instance, when it reads froma socket or prints to it the program can’t do anything. The possible solutionis to delegate working with clients to separate process/threads, creating anyprocess and swap contexts between them is not a cheap operation. To solve thisissue, there is a so-called asynchronous way of working with sockets. The mainidea is to maintain the socket’s state to an OS and check the program whenthere is something to read from the socket or when it is ready for writing.

 

Sataware Technologies isone of the leading Mobile App Development Company

We’re specialist in areassuch as Custom Software Development, Mobile App Development, Ionic ApplicationDevelopment, Website Development, E-commerce Solutions, Cloud Computing,Business Analytics, and Business Process Outsourcing (Voice and non-voiceprocess) We believe in just one thing – ON TIME QUALITY DELIVER

 

TAGS:

App development company

Software developmentcompany

Game development company

 

OUR SERVICES:

• Software Development

• Mobile App Development

• Web Development

• UI/UX Design andDevelopment

• AR and VR AppDevelopment

• IoT ApplicationDevelopment

• Android App Development

www.sataware.com/app-developers/

• Windows App Development

 

CONTACT DETAILS:

Sataware Technologies

+1 5204454661

[email protected]

Contact us:www.sataware.com

 

ADDRESS:

1330 West, Broadway Road,

Tempe, AZ 85282, USA

 

Sataware Technologies has plotted a global presence in various places including the USA, Australia, and the UK. Ashok Marimuthu is the man who has behind the success story of Sataware Technologies. His dedicated and innovative tactics, coupled with the team’s result-driven tactics have taken the Sataware to a position of prominence.