Which module is the best for Python network programming? Why is it the best? This article will give an example of a detailed explanation of the Python module.
Python Internet Module
The following lists some important modules for Python network programming: Below, a few Python modules are explained in detail:Simple example
Server
#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 of the service by calling the bind(hostname, port) function.
Next, we call the accept method of the socket object. This method waits for the client's connection and returns the connection object, indicating that it is connected to the client.
The complete code is as follows:
#!/usr/bin/python #-*-coding:UTF-8-*- #文件名:server.py importsocket #导入 socket 模块 s=socket.socket() #创建 socket 对象 host=socket.gethostname() #获取本地主机名 port=12345 #设置端口 s.bind((host, port)) #绑定端口 s.listen(5) #等待客户端连接 while True: c, addr = s.accept() # 建立客户端连接。 print '连接地址:', addr c.send('欢迎访问php中文网!') c.close() # 关闭连接
The above is the detailed content of Which module is best for Python network programming? Detailed explanation of Python modules with examples. For more information, please follow other related articles on the PHP Chinese website!