Home > Backend Development > Python Tutorial > Which module is best for Python network programming? Detailed explanation of Python modules with examples

Which module is best for Python network programming? Detailed explanation of Python modules with examples

Tomorin
Release: 2018-08-16 17:35:43
Original
2418 people have browsed it

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:

Which module is best for Python network programming? Detailed explanation of Python modules with examples

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()        # 关闭连接
Copy after login



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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template