Home > Article > Backend Development > How to develop with Python client? Detailed explanation of Python modules with examples
This article is similar to the previous "Which module is best for Python network programming?" For example, "Detailed Explanation of Python Modules" is linked to introduce the development of the server and Python client under Python statements. I hope it can help you.
Client: The party that contacts the server for a specific request, receives services and handles related transactions. The client can continue to send requests to the server, or it can no longer send requests after ending the transaction request.
Next we write a simple Python client instance to connect to the service created above. The port number is 12345.
socket.connect(hosname, port) method opens a TCP connection to the service provider whose host is hostname and port is port. After connecting, we can get data from the server. Remember, the connection needs to be closed after the operation is completed.
The complete code is as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:client.pyimport socket # 导入 socket 模块 s = socket.socket() # 创建 socket 对象 host = socket.gethostname() # 获取本地主机名 port = 12345 # 设置端口好 s.connect((host, port)) print s.recv(1024) s.close()
Now we open two terminals, the first terminal executes the server.py file:
$ python server.py
The second terminal executes the client.py file :
$ python client.py 欢迎访问php中文网!
At this time, when we open the first terminal, we will see the following information output:
Connection address: ('192.168.0.118', 62461)
The above is the detailed content of How to develop with Python client? Detailed explanation of Python modules with examples. For more information, please follow other related articles on the PHP Chinese website!