在我们之前的教程中,我们讨论了 Python unittest模块,今天我们将研究 Python 插件编程示例,我们将创建 Python 插件服务器和客户端应用程序。
Python Socket 编程
要了解python接口编程,我们需要了解三个有趣的话题 - Socket Server, Socket Client和 Socket. 所以,什么是服务器?好吧,服务器是等待客户端请求的软件,并相应地服务或处理它们。另一方面,客户端是该服务的请求者。 客户端程序向服务器请求一些资源,服务器响应该请求。 接口是服务器和客户端之间的双向通信渠道的终端。 接口可以在一个过程内进行通信,在同一台机器上的过程之间,或在不同的机器上的过程之间进行处理。 对于任何与远程程序的通信,我们必须通过接口连接。 这个接口编程教程的主要目的是向您介绍如何接口服务器和客户端相互通信。 您
Python 插件示例
我们之前已经说过,一个接口客户端向接口服务器请求一些资源,而服务器会响应这个请求,所以我们将设计服务器和客户端模型,以便每个人都能与它们进行通信。
- Python 接口服务器程序首先执行并等待任何请求
- Python 接口客户端程序将首先启动对话 3.然后服务器程序将相应地响应客户端请求
- 客户端程序将终止如果用户输入
再见
消息。
Python Socket 服务器
我们会将 python 接口服务器程序保存为 socket_server.py
. 要使用 python 接口连接,我们需要导入 socket 模块. 然后,连续地我们需要执行一些任务来建立服务器和客户端之间的连接。 我们可以通过使用 socket.gethostname()
函数获取主机地址。 建议用户端口地址高于 1024,因为端口号小于 1024 被保留给标准的互联网协议。 请参阅下面的 python 接口服务器示例代码,评论将帮助您理解代码。
1import socket
2
3def server_program():
4 # get the hostname
5 host = socket.gethostname()
6 port = 5000 # initiate port no above 1024
7
8 server_socket = socket.socket() # get instance
9 # look closely. The bind() function takes tuple as argument
10 server_socket.bind((host, port)) # bind host address and port together
11
12 # configure how many client the server can listen simultaneously
13 server_socket.listen(2)
14 conn, address = server_socket.accept() # accept new connection
15 print("Connection from: " + str(address))
16 while True:
17 # receive data stream. it won't accept data packet greater than 1024 bytes
18 data = conn.recv(1024).decode()
19 if not data:
20 # if data is not received break
21 break
22 print("from connected user: " + str(data))
23 data = input(' -> ')
24 conn.send(data.encode()) # send data to the client
25
26 conn.close() # close the connection
27
28if __name__ == '__main__':
29 server_program()
如果您希望服务器在客户端连接关闭时不会停止,只需删除 if condition并打破声明。
Python Socket 客户端
我们会将 python socket 客户端程序保存为 socket_client.py
. 这个程序与服务器程序类似,除了绑定之外。 服务器和客户端程序的主要区别在于,在服务器程序中,它需要将主机地址和端口地址结合在一起。 参见下面的 python socket 客户端示例代码,评论将帮助您理解代码。
1import socket
2
3def client_program():
4 host = socket.gethostname() # as both code is running on same pc
5 port = 5000 # socket server port number
6
7 client_socket = socket.socket() # instantiate
8 client_socket.connect((host, port)) # connect to the server
9
10 message = input(" -> ") # take input
11
12 while message.lower().strip() != 'bye':
13 client_socket.send(message.encode()) # send message
14 data = client_socket.recv(1024).decode() # receive response
15
16 print('Received from server: ' + data) # show in terminal
17
18 message = input(" -> ") # again take input
19
20 client_socket.close() # close the connection
21
22if __name__ == '__main__':
23 client_program()
Python Socket 编程输出
To see the output, first run the socket server program. Then run the socket client program. After that, write something from client program. Then again write reply from server program. At last, write bye from client program to terminate both program. Below short video will show how it worked on my test run of socket server and client example programs.
1pankaj$ python3.6 socket_server.py
2Connection from: ('127.0.0.1', 57822)
3from connected user: Hi
4 -> Hello
5from connected user: How are you?
6 -> Good
7from connected user: Awesome!
8 -> Ok then, bye!
9pankaj$
1pankaj$ python3.6 socket_client.py
2 -> Hi
3Received from server: Hello
4 -> How are you?
5Received from server: Good
6 -> Awesome!
7Received from server: Ok then, bye!
8 -> Bye
9pankaj$
请注意,接口服务器在端口5000上运行,但客户端还需要接口端口连接到服务器。该端口由客户端连接调用随机分配。在这种情况下,它是57822。所以,这就是Python接口编程,Python接口服务器和接口客户端示例程序。