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: 439
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: 255Word Count: 268See All articles From Author

Add Comment

Beauty Articles

1. Wedding Bridal Makeup Artist In Ahmedabad
Author: lafemmeindia

2. Achieve 100% Visible Results: The Best Acne And Pimple Treatment At Valeda Hair And Skin Clinic With Dr. Seema Bali
Author: Valedaherbal

3. Achieve The Perfect Look: Tips From Naser Haircuts
Author: naser haircuts

4. Why Physiotherapy Is Essential For Long-term Pain Relief And Prevention?
Author: Jayceon

5. Peptide Therapy For Anti-ageing: A Revolutionary Approach To Youthful Skin
Author: Ann Joseph

6. Which Type Of Suit Is Trending In India?
Author: Shabana Bano

7. A Comprehensive Guide To Dermal Fillers
Author: Sanchi

8. Discover The Best Beauty Parlour In Entally, Kolkata – Payal Beauty Parlour
Author: Subhajit

9. Teenagers Often Look For Advice On Personal Style And Beauty Products
Author: TAHIR LATEEF

10. Five Top Beauty Enhancement Products Along With Short Details:
Author: TAHIR LATEEF

11. Future Outlook For The India Automotive Stamping Industry
Author: mmr

12. Finding The Perfect Indian Bridal Hair And Makeup Artist In London For Your Big Day
Author: Tina Prajapat Ltd

13. How Anti-ageing Facials Can Transform Your Skin And Restore A Youthful Glow
Author: Ankita B Dhingra

14. Regain Confidence With Hair Transplants In Riyadh
Author: MONA

15. Unlock The Secrets Of Makeup Bronzer: The Ultimate Guide To Radiant, Sculpted Skin
Author: HOK Makeup

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