Table of Contents
Let us create a socket called socket_server.py, and then enter the following code: " >socket serverLet us create a socket called socket_server.py, and then enter the following code:
After creating socket_server.py, we can create our client. Let us create a file called socket_client.py, and then enter the following code: " >socket clientAfter creating socket_server.py, we can create our client. Let us create a file called socket_client.py, and then enter the following code:
Home Backend Development Python Tutorial python socket completes simple communication

python socket completes simple communication

Dec 25, 2020 pm 05:29 PM
python socket

python tutorialThe column introduces the socket communication method

python socket completes simple communication

Recommended (free): python tutorial

Introduction to socket

socket is also known as " Socket", the socket will send data through the udp/tcp protocol to achieve simple communication between two machines.

Note: If you want to use socket to achieve simple communication between two machines, please first ensure that the two machines are connected to the same local network. Of course, socket can also realize communication between a machine. You only need to set the connection object IP to 127.0.0.1, which is the local IP.

Examples

Only some practical functions are shown here.


##Function Description socket.socket()Create a socketsocket.bind()Bind to an ip and port, pass in Parameters are tuplessocket.recv()Accept datasocket.send()Send datasocket.close()Close the socketsocket.connect()Connect to an ip and port##socket.listen()socket.accept()
Enable listening mode on the tcp port
Blocking, waiting for connection
Communication function between machines

import socket
server = socket.socket()server.bind(('0.0.0.0',80))server.listen()sock,addr = server.accept()data = ""while True:
    tmp_data = sock.recv(1024)
    if tmp_data:
        data += tmp_data.decode("utf8")
    else:
        breakprint('%s发送的内容:%s'%(addr[0],data))sock.close()
Copy after login

Here we create a socket and bind it to 0.0. At the address of 0.0:80, this address can also be changed to the name of our local machine. Then we start listening mode. After the user connects, we start receiving data (note: utf8 decoding is required before accepting data).

import socket
client = socket.socket()client.connect(('127.0.0.1',80))client.send("Hello,Server.".encode("utf8"))client.close()
Copy after login

Here we will only talk about the following two functions: connect and send. We passed in a tuple to the connect function, but of course a list will also work. The first element needs to be the connected object IP, and the second is the port. After the connection is completed, we use the send function to send the message. Before sending, we need to encode the content into utf8 type.

Send data to a websiteWe create a file called socket_website.py and enter the following code:

import socket
s.connect(('www.baidu.com',443))s.send('HELLO'.encode('utf8'))s.close()
Copy after login

Here, we sent data to baidu.com. Since Baidu uses https protocol, we use port 443. If the code does not report an error, it means the sending was successful. At this time, Baidu's database will have an additional text content data called HELLO.

tip: If you continuously use socket to send data to a website or machine, too much data will cause the target database/machine memory to become full, leading to a crash. This implements a simple legendary ddos ​​attack

The above is the detailed content of python socket completes simple communication. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles