Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use socket in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use socket in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to use socket in python"!

01: OSI Model

1) OSI model includes 7 layers: application layer, presentation layer, session layer, transport layer, network layer, data link layer and physical layer. The most commonly used are the application layer and the transport layer, and the rest are not introduced one by one.

2) Application layer: network services and some terminal applications. such as HTTP, FTP,

3) Transport layer: defines some protocols and port numbers for transmitting data. such as UDP protocol, TCP protocol

02: Three elements of network communication (IP address, port number, transmission protocol)

1) IP address: indicates an independent host on the network; IP address = network address + host address. Among them, the special IP address: 127.0.0.1(local loopback address) indicates the local machine.

2) Port number: a logical address used to identify a process; different processes have different port identifiers.

3) Transmission protocol: rules for communication.

UDP: User Datagram Protocol. Connectionless oriented, the source and destination do not need to establish a connection before data can be transmitted. Such as real-time online chat, video conferencing, etc.

TCP: Transmission Control Protocol. Connection-oriented, where a connection needs to be established before data can be transmitted. A large amount of data transmission is carried out during the connection process, and the connection is completed by means of "three-way handshake", which is a safe and reliable protocol. Slow transmission speed and low efficiency

03: TCP three-way handshake

1) TCP establishes a connection to transmit three packets, commonly known as three-way handshake (Three-way handshake) popular understanding as:

Socket A: "Hello, Socket B, I have data to send to you, establish a connection. Socket B: "Okay, I'm ready. Socket A: Thank you for accepting my request. "

2) The three-way handshake is completed, the TCP client and server successfully establish a connection, and the data can be transmitted.

04: Socket communication flow

1) Create a server-side socket object. When an object is created, it needs to bind its IP address (IPv4 or IPv6) and protocol (TCP or UDP). bind() binds the native IP address and port number,listen() starts listening for requests sent by clients. The server is now complete.

2) Create a server-side socket object. An attempt is made to connect to the server socket based on the server IP address and port number.

3) The server listens to receive accept() requests sent by the client and establishes a connection with the client connect().

4) After the connection is established, the client can send() data to the socket object, and the server can receive recv() data sent to the client.

5) After the server processes the data, it can return a send() processing result to the socket object, and the client reads recv() to this return result.

6) The client closes the close() connection, and the client releases the currently occupied client port number.

7) After the server receives the client close message, close the connection with the client

05: Socket common method

1) Overview: Also known as "socket", applications usually make requests to the network or respond to network requests through "socket", so that between hosts or between processes on a computer can communicate.

2) Method classification: Server socket, Client socket, Public socket

① Server: bind, listen, accept

② Client: connet, etc.

③ Common end: recv, send, sendall, close, etc.

3) Note: socket enters blocking state, i.e. accept() method waits until the client returns connection information before returning, and starts receiving the next client connection request.

06: Simple communication between server and client

Send data: transfer by bytes type, need to be converted by bytes(sendData)

Receive data: receive via str type, need to convert via str(client_data)

accept(): Accepts and establishes a connection to the client, where the program starts blocking and only connects to the client.

#server-side code import socket sk=socket.socket()#create server-side socket object ip_port=('127.0.0.1', 13008)sk.bind(ip_port)#bind IP address and port number sk.listen(5)#start listening for requests: up to 5 connections allowed at the same time print ('Wait for client connection')#Release signal coon,addr=sk.accept()#A new socket and client address print will be returned after successful connection ('client new connection:', addr)client_data=coon.recv(1024)#receive data: Receive 1024 bytes print at once (str(client_data,encoding='utf8'))sendData=input ('please enter>>>')coon.sendall (bytes(sendData,encoding ='utf8 '))#send data coon.close()#close resource sk.close()#client code import socket sk=socket.socket()#create client socket object ip_port=('127.0.0.1', 13008)sk.connect(ip_port)#initiate connection sendData=input ('please enter>>>')sk.sendall(bytes(sendData,encoding ='utf8 '))#send data: transmit via bytes type sever_data=sk.recv(1024)#receive data: receive 1024 bytes print(str(sever_data,encoding ='utf8'))#receive data: receive sk.close() via str type At this point, I believe you have a deeper understanding of "how to use socket in python", you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report