Table of Contents
Client
Server
Home Backend Development Python Tutorial How python implements client-side and server-side data transmission (code)

How python implements client-side and server-side data transmission (code)

Sep 06, 2018 pm 05:53 PM
linux python

The content of this article is about how Python implements client-side and server-side data transmission. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Client

def sock_client_data():
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('192.168.20.1', 6666))  #服务器和客户端在不同的系统或不同的主机下时使用的ip和端口,首先要查看服务器所在的系统网卡的ip
            # s.connect(('127.0.0.1', 6666))  #服务器和客户端都在一个系统下时使用的ip和端口
        except socket.error as msg:
            print(msg)
            print(sys.exit(1))
        data = input("input data:")   #输入要传输的数据
        s.send(data.encode())  #将要传输的数据编码发送,如果是字符数据就必须要编码发送
        s.close()
if __name__ == '__main__':
    sock_client_data()
Copy after login

Server

def socket_service_data():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('127.0.0.1', 6666))  # 在同一台主机的ip下使用测试ip进行通信
        # s.bind(('192.168.20.1', 6666))  #在不同主机或者同一主机的不同系统下使用实际ip
        s.listen(10)
    except socket.error as msg:
        print(msg)
        sys.exit(1)

    print("Wait for Connection..................")

    while True:
        sock, addr = s.accept()
        buf = sock.recv(1024)  #接收数据
        buf = buf.decode()  #解码
        print("The data from " + str(addr[0]) + " is " + str(buf))
        print("Successfully")
        # return buf
        # sock.close()
if __name__ == '__main__':
    socket_service_data()
Copy after login

Run result:

Client:

How python implements client-side and server-side data transmission (code)

Server side:

How python implements client-side and server-side data transmission (code)

##Instructions:

1. Server and client Duantong uses 127.0.0.1 in the same system of a physical host. The port is set by itself, as long as it is not occupied. Under the Linux system, check whether the port is occupied with the command sudo netstat -nap | grep 6666. If the port cannot be found, prove the port. Not occupied. Create two .py files, one is sender.py, which stores the client program, and the other is recieve.py, which stores the server program. Run the server program first, then run the client program after the connection is successful, and enter the data to be transmitted. , when you see the transmitted data on the server terminal, the transmission is successful.
2. If the server and client are in different systems on the same physical host, use the actual IP address of the server system. The author uses Windows as the client and Linux as the server. Enter ifconfig in the Linux terminal to view the IP address. Two The program uses the same IP and port, and deploys the two programs to their respective systems. First, run the server-side program, then run the client-side program, send data, and wait until the server-side data is successfully received.

Related recommendations:

Simple file transfer server and client implemented in Python

php image upload client and server End implementation method,

The above is the detailed content of How python implements client-side and server-side data transmission (code). 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Is Debian Strings worth using Is Debian Strings worth using Apr 02, 2025 am 08:24 AM

Regarding the choice of Debian system, this article will explore the advantages of Debian system and help you determine whether it is suitable for your needs. The image shows the missing search results related to DebianStrings, which prompts us to look at the Debian system from a broader perspective. Debian Community & Stability: Debian is known for its large and active open source community, which means you can easily access a large amount of documentation, tutorials, and community support to quickly resolve issues you encounter. The stability of the system is also a highlight of Debian, especially in the server environment, which is much more stable than other distributions. Debian vs. Ubuntu: Compared with Ubuntu, Debian

How is Debian Hadoop compatibility How is Debian Hadoop compatibility Apr 02, 2025 am 08:42 AM

DebianLinux is known for its stability and security and is widely used in server, development and desktop environments. While there is currently a lack of official instructions on direct compatibility with Debian and Hadoop, this article will guide you on how to deploy Hadoop on your Debian system. Debian system requirements: Before starting Hadoop configuration, please make sure that your Debian system meets the minimum operating requirements of Hadoop, which includes installing the necessary Java Runtime Environment (JRE) and Hadoop packages. Hadoop deployment steps: Download and unzip Hadoop: Download the Hadoop version you need from the official ApacheHadoop website and solve it

How to check code style on Flutter on Debian How to check code style on Flutter on Debian Apr 02, 2025 am 08:18 AM

This article describes how to use Flutter on Debian system for Dart code style checking to ensure the consistency and readability of the code. First, make sure Flutter is installed. If not installed, please refer to the official document: https://flutter.dev/docs/get-started/install/linux for installation. Next, open the terminal and use the cd command to enter the root directory of your Flutter project. Execute code style check, use the command: the flutteranalyze terminal will display the check results, including the file, line number and description where the problem is located. Some problems can be automatically fixed by using the following command: flut

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

How safe is Golang logs in Debian How safe is Golang logs in Debian Apr 02, 2025 am 08:27 AM

As a safe and reliable programming language, Golang has built-in many features and third-party libraries to assist developers in building secure applications. When running Golang programs on Linux distributions such as Debian, log security mainly depends on the following key factors: 1. Efficient and secure log concurrent processing Golang provides multiple mechanisms to process concurrent log writing to avoid data competition: Channel: Use channels to safely transmit log information between multiple goroutines, effectively preventing concurrent write conflicts. Mutex: Ensure that only one goroutine is written to the log at the same time, but may reduce performance slightly. Lock-free queue (Lock-freeq)

How to implement operations on Linux iptables linked lists in Golang? How to implement operations on Linux iptables linked lists in Golang? Apr 02, 2025 am 10:18 AM

Using Golang to implement Linux...

Does XML modification require programming? Does XML modification require programming? Apr 02, 2025 pm 06:51 PM

Modifying XML content requires programming, because it requires accurate finding of the target nodes to add, delete, modify and check. The programming language has corresponding libraries to process XML and provides APIs to perform safe, efficient and controllable operations like operating databases.

How to define an enum type in protobuf and associate string constants? How to define an enum type in protobuf and associate string constants? Apr 02, 2025 pm 03:36 PM

Issues of defining string constant enumeration in protobuf When using protobuf, you often encounter situations where you need to associate the enum type with string constants...

See all articles