123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Beauty >> View Article

How To Work With Tcp Sockets In Python

Profile Picture
By Author: Appdeveloper
Total Articles: 438
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

A network/internet socket is an end-point of interposing communication across a computer network. In Python web development, the library has a module called socket which offers a low-level networking interface, it’s common across various app development programming languages since it uses OS-level system calls. When you create a socket, use a function called a socket. 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 the type 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, bind it to localhost and 50000 port, and start listening for incoming app development connections. To accept an app developers incoming connection call as accept(), this method will block until a new client connects. When it’s happening, will create a new socket and returns it together with the iOS app developers client’s address. Then, an infinite loop reads data from the socket in batches of 1 KB using method recv() till it returns a blank string. Next, it sends all incoming data back using a method called sendall() which inside repeatedly calls send(). Next, to that, it just closes the client’s connection.

Total Views: 549Word Count: 268See All articles From Author

Add Comment

Beauty Articles

1. How To Choose The Best Makeup Artist Northwest London For Bridal Beauty, Special Occasions And Flawless Make-up Looks
Author: Tina Prajapat

2. Key Steps To Prevent Damage When Carrying Food Items In Commercial Trucks
Author: Amay Guru

3. How To Get Your Original Skin Tone Back: Expert-backed Ways To Restore An Even Complexion
Author: Vaibhav Sharma

4. Why Inspired Perfume Oils Are Becoming A Popular Choice For Fragrance Enthusiasts
Author: Best Attar

5. Beauty Salon In Al Qusais: How To Maintain Smooth Hair After A Salon Treatment
Author: Naina Singh

6. Best Hair Oil For Hair Fall: What To Check Before
Author: Nurture By Anita

7. Is The Best Salon In Al Barsha Right For Your Beauty Routine? Blush N Curls Ladies Salon & Spa Explains
Author: Naina Singh

8. Beauty Salon In Al Mankhool: Signs Your Hair Needs Professional Frizz Treatment At Blush N Curls Ladies Salon & Spa
Author: Naina Singh

9. Cut Drying Time And Get A Salon Finish With Argan And Linseed Oil
Author: Simel hair

10. Choosing The Right Tattoo Artist For Complex Custom Designs
Author: gandhiaditi01

11. Best Ai Interior Design Tools For Planning Modern Homes
Author: venkateshvenky432

12. Best Hair Fall Treatment In Kukatpally For Stronger And Healthier Hair
Author: venkateshvenky432

13. Teen Acne Worries? A Nanuet Acne Facial Parents Can Feel Good About
Author: Rose Ela

14. How To Use Face Oil For Glowing Skin: A Simple Guide For Beginners
Author: Nurture By Anita

15. Best Beauty Salon In Al Qusais: Signs Your Hair Needs Professional Colour Correction
Author: John

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: