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 Python to create a chat channel that only belongs to you and ta

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to use Python to make a chat channel that only belongs to you and ta. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Are you worried that the data flow of Wechat will be monitored? Are you worried that your little secret of chatting with ta will be saved in some database? It doesn't matter, now we can use Python as a chat channel that only belongs to you and ta to allay your worries. After all, only what you do is the most reassuring.

1. Brief introduction of principle

In today's tutorial, we will use the concept of instant messaging, which allows two or more people to use the network to transmit text messages, text, voice, and so on. Instant messaging is generally based on socket connections, which can be used to send or receive data. The general combination is the IP+ port number.

That is to say, in our example, one side of the chat should assume the responsibility of the "server", maintaining a socket server and waiting for the connection to enter, while the other side is the "client". When the server maintains a waiting state, it can send a request and establish a connection.

When you and ta want to enter the "small dark room" to chat, only one side acts as the server, and the other acts as the client. As the "server side" person, you can tell each other the IP and port number in Wechat, and then build a connection and chat in the small dark room. The data in this small dark room will not be retained by any database (unless you have made a saved database yourself).

two。 Code writing

All right, we've made the basics clear. However, before you start the tutorial, you need to install Python, and if you haven't already, you can read this article: Python Hyperdetailed installation Guide

2.1 Server side

When chatting, we sometimes encounter both sides sending messages at the same time. This kind of chat is called full-duplex chat: the "server" can send messages to the "client", and the "client" can send messages to the "server" and allow messages to be sent at the same time.

How does the server achieve full-duplex chat? In fact, it is very simple, as long as you use multithreading. The main thread is used to receive the connection from the client. After a successful connection, two new threads are created: one for sending messages and the other for receiving messages:

First, set up a socket server:

Where AF_INET refers to the use of IPv4 for communication, and SOCK_STREAM refers to the TCP protocol. You can set the port number as you like, and the IP address on the server side is empty by default.

Constantly waiting for the user to connect in the while loop. If a user connects successfully, we will move on to the next step, establishing the sending and receiving threads, respectively:

Clientsock is the socket connection we get. ProcessRecv and processSend are used to process receiving messages and sending messages respectively:

One small detail to note is that the sendall function of the socket connection only supports data of type bytes, so we want to encode ('utf-8').

All the code on the server side is like this, yes, it is that simple.

2.2 client

The client is simpler, and the main thread itself is set to accept messages, so we only need one more thread to send messages. The full code of the client is as follows:

Where the port number is entered in the HOST part and the IP,PORT part of the other party. Sys.argv is used to enter these two values through parameters. For example, we name the client file client.py and enter it in cmd:

Python client.py 127.0.0.1 51423

You can directly pass in parameters to execute the script, but other parts are actually similar to those on the server. Pay attention to decode the received data (because we sent it encode).

3. Improve

In fact, although this code can be used, there are still many problems. For example, when you are chatting, suddenly another person sends a connection request to the server? At this time, we need to add a code with authentication on the server side and ask the other party to enter the chat room password before we can establish a connection.

This password must be known to both of you, and any act of spreading the password through a third-party tool is unreliable and insecure. Only in this way can we prevent the eavesdropping of a third party. In fact, it is not difficult to add a password function, this part is left to everyone to achieve!

On how to use Python to make only you and ta chat channel to share here, I hope the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report