Home  >  Article  >  Backend Development  >  python socket completes simple communication

python socket completes simple communication

coldplay.xixi
coldplay.xixiforward
2020-12-25 17:29:196222browse

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

socket serverLet us create a socket called socket_server.py, and then enter the following code:

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()

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).

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:

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

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()

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:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete