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 realize the connection between server and client in Python

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

Share

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

This article mainly explains "how to realize the connection between the server and the client in Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to connect the server and the client in Python.

Server side

We use the socket function of the socket module to create a socket object. The socket object can set up a socket service by calling other functions.

Now we can specify the port (port) of the service by calling the bind (hostname, port) function.

Next, we call the accept method of the socket object. This method waits for a connection from the client and returns a connection object indicating that it is connected to the client.

The complete code is as follows:

Example

#! / usr/bin/python3

# File name: server.py

# Import socket and sys modules

Import socket

Import sys

# create socket object

Serversocket = socket.socket (

Socket.AF_INET, socket.SOCK_STREAM)

# get the local hostname

Host = socket.gethostname ()

Port = 9999

# bind port number

Serversocket.bind ((host, port))

# set the maximum number of connections and queue after that

Serversocket.listen (5)

While True:

# establish a client connection

Clientsocket,addr = serversocket.accept ()

Print ("connection address:% s"% str (addr))

Msg=' Welcome to the rookie tutorial!' + "\ r\ n"

Clientsocket.send (msg.encode ('utf-8'))

Clientsocket.close ()

Client

Next, let's write a simple client instance to connect to the service created above. The port number is 9999.

The socket.connect (hostname, port) method opens a TCP connection to a service provider whose host is hostname and the port is port. After the connection, we can get the data from the server. Remember, the connection needs to be closed after the operation is completed.

The complete code is as follows:

Example

#! / usr/bin/python3

# File name: client.py

# Import socket and sys modules

Import socket

Import sys

# create socket object

S = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

# get the local hostname

Host = socket.gethostname ()

# set port number

Port = 9999

# Connect service, specify host and port

S.connect ((host, port))

# receive data of less than 1024 bytes

Msg = s.recv (1024)

S.close ()

Print (msg.decode ('utf-8'))

Now let's open two terminals, and the first terminal executes the server.py file:

$python3 server.py

The second terminal executes the client.py file:

$python3 client.py Welcome to the rookie tutorial!

At this point, when we open the first terminal, we will see the following information output:

Connection address: ('192.168.0.118, 33397) so far, I believe you have a deeper understanding of "how to connect the server and the client in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow 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

  • How to understand custom events of components in Vue

    This article shows you how to understand the custom events of components in Vue, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article. {{msg}} < / h3 >

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

    12
    Report