A detailed explanation of the handshake between the client and the server of python Socket, pythonsocket

Source: Internet
Author: User

A detailed explanation of the handshake between the client and the server of python Socket, pythonsocket

In simple learning, socket is used to establish a connection between the client and the server and send data.

1. Client socketClient. py code

Import socket s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # establish a connection: s. connect ('2017. 0.0.1 ', 9999) # receive the welcome message: print (s. recv (1, 1024 ). decode ('utf-8') for data in [B 'Michael', B 'Tracy', B 'Sarah']: # send data: s. send (data) print (s. recv (1, 1024 ). decode ('utf-8') s. send (B 'exit ') s. close ()

2. Server serverSocket. py code

Import socket import threading import time # from threading import Thread s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # listening port: s. bind ('2017. 0.0.1 ', 9999) s. listen (5) print ('Waiting for connection... ') def tcplink (sock, addr): print ('Accept new connection from % s: % s... '% addr) sock. send (B 'Welcome! ') While True: data = sock. recv (1024) time. sleep (1) if not data or data. decode ('utf-8') = 'eg': break sock. send ('hello, % s! '% Data. decode ('utf-8 ')). encode ('utf-8') sock. close () print ('Connection from % s: % s closed. '% addr) while True: # accept a new connection: sock, addr = s. accept () # create a new thread to process TCP connections: t = threading. thread (target = tcplink, args = (sock, addr) t. start ()

3. Operation Process

Open two console windows and run python3 serverSocket. py on the server first.

Then run the client python3 socketClient. py

The socket connection is as follows:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.