Home > Backend Development > Python Tutorial > Detailed explanation of commonly used modules for network programming in Python3

Detailed explanation of commonly used modules for network programming in Python3

黄舟
Release: 2017-08-09 10:52:50
Original
2233 people have browsed it

1. Socket module

Network services are based on sockets. Sockets are network connection endpoints and the foundation of the network; each Sockets are bound to the specified IP and port;

1. First use the socket(family=AF_INET,type=SOCK_STREAM,proto) function to create an object;

family            地址参数,还可以有AF_INET6,AF_UNIX;
  type               socket类型;
  proto             协议类型,可选参数
Copy after login

After successful creation Use bind('127.0.0.1',1051) to bind the ip address and port. If the address is empty, it means the local machine;

2. Socket object method:

listen(backlog)              监听所有socket对象创建的连接,backlog指定连接队列数,最小为1,最大一般为5;
connect(address)          连接到服务端
connect_ex(address)     两个都可以连接到服务端,不同的是第一个返回一个错误,第二个返回一个异常;
accept()                        接收来自客户端的数据,返回一个新的socket对象和客户端地址;
recv(bufsize,flags)         仅返回所接收的字符串;bufsize指定接收缓冲区的大小,flags为可选参数,表示接收标志;
recvfrom(bufsize,flags) 返回所接收的字符串和地址;
send(string,flags)          向已经连接的socket发送数据;
sendall(string,flags)      与send不同的是将会一直发送完全部数据;
sendto(string,flags,address) 可以向一个未连接的socket发送数据;
makefile(mode,bufsize)        将socket关联到文件对象上,两个参数都是可选的,mode文件模式,bufsize缓冲区大小;
close()                          完成通信后,应使用close方法关闭网络连接;
Copy after login

2. httplib module

(1) The httplib module provides HTTPConnection objects and HTTPRresponse objects;

The available methods when creating an HTTPConnection object are:

1、request(method,url,body,headers)     向服务器发送请求;
       method  方法,有"GET","POST"等待连接
       body       发送的数据
       headers  发送的HTTP头
2、getresponse() 返回一个HTTPResponse对象;
3、close()            关闭与服务器的连接;
4、send(data)     发送数据;
5、putrequest(request,selector,skip_host,skip_accep_encoding) 向服务器发送请求;
         request          所发送的操作;
         selector          进行操作的URL;
         skip_host       若为True则禁止自动发送"POST";
         skip_accep_encoding 若为True则禁止自动发送"Accept-Encoding:headers"
6、putheader(headers,argument,...)
         headers         发送的HTTP头;
         argument      发送的参数;
7、endheaders()
Copy after login

(2) HTTPResponse Object method:

1、read()                   获得服务器的响应主体;
 2、getheader(name,default)  获取服务器响应的HTTP头;
 3、version()               查看HTTP协议的版本;
 4、status()                 查看HTTP协议的状态;
 5、reason()
Copy after login

3. ftp module

1, FTP (host,user,passwd,acct) Create an FTP connection object. The methods of this object are:

getwelcome()                      获得FTP服务器的欢迎信息
       abort()                                中断文件传输
       sendcmd(command)           发送命令,command为一个字符串
       voidcmd(command)            发送命令,但没有返回值
       retrbinary(command,callback,maxblocksize,rest) 下载文件(二进制)
              command             由"RETR 文件名  组成"
              callback                回调函数
              maxblocksize        每次传输最大字节数
              rest                       文件续传位置
       retrlines(command,callback)              下载文件(ASCII)
       storbinary(command,file,blocksize)    以二进制上传文件;
       storlines(command,file)                     以ASCII形式上传文件;
       dir()                                                   获取当前目录的内容列表;
       rename(fromname,toname)               重命名
       delete(filename)                                删除文件
       cwd(pathname)                                 改变当前目录
       mkd(pathname)                                创建目录
       rmd(dirname)                                    删除服务器上的目录
       size(filename)                                    获取文件大小
       set_pasv(boolean)                            设置传输模式
       quit()                          
       close()                                               关闭服务器的连接
2、set_debuglevel(level)           设置调试级别
3、connect(host,port)                配置host
4、login(user,passwd,acct)        登录
Copy after login

Attachment: You can use the poplib module and smtplib module to send and receive emails

The above is the detailed content of Detailed explanation of commonly used modules for network programming in Python3. For more information, please follow other related articles on the PHP Chinese website!

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