Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the connection between server and client in Python

Shulou Source: shulou.com Published: 2022-06-01 01:33:18 09月16日 Update

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!

Tags: Services clients clients objects hosts files methods terminals functions slogans instances modules ports code content available through address tutorial data file name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Apple vpn Xiaomi