1. Email:
Email history:
发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人
Write MUA to send the message to the MTA:
The protocol used by MUA and MTA is Smtp:simple Mail Transfer Protocol.
When you send a message, configure the SMTP server (on which MTA), such as the SMTP server address provided by 163: smtp.163.com。,SMTP服务器还要求你填写邮箱地址和邮箱密码
Write mua to receive messages from the MDA:
There are two types of protocols used by MUA and MDA:POP: Post Office Protocol (commonly known as POP3) IMAP: Internet Message Access Protocol
MDA server also requires you to verify your mailbox password when you receive e-mail
smtp Send mail: SMTP Email protocol
email模块:
Responsible for structuring the message
fromEmail.mime.textImportMimetext##第一个参数邮件正文, the second parameter MIME subtype (' Plain ' for plain text), UTF-8 encoding guarantees multi-language compatibilitymsg = Mimetext ('Hello, send by Python ...','Plain','Utf-8')#Enter your email address and password:FROM_ADDR = input ('From :') Password= Input ('Password:')#Enter recipient address:TO_ADDR = input ('To :')#Enter the SMTP server address:Smtp_server = input ('SMTP Server:')ImportSmtplibserver= Smtplib. SMTP (Smtp_server, 25)#the SMTP protocol default port isServer.set_debuglevel (1)#Print out all information that interacts with the SMTP serverServer.login (from_addr, password)#log on to the SMTP serverServer.sendmail (FROM_ADDR, [to_addr], msg.as_string ())#send an email to more than one person to pass in a list, the body is a str,as_string () to turn the Mimetext object into STRserver.quit ()
Full message:
fromEmailImportencoders fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, FormataddrImportSmtplibdef_format_addr (s): # _format_addr()
to format an email address name, addr=parseaddr (s)returnFormataddr (Header (name,'Utf-8'). Encode (), addr) from_addr= Input ('From :') Password= Input ('Password:') to_addr= Input ('To :') Smtp_server= Input ('SMTP Server:') msg= Mimetext ('Hello, send by Python ...','Plain','Utf-8') msg[' from'] = _FORMAT_ADDR ('python enthusiasts <%s>'%from_addr) msg[' to'] = _FORMAT_ADDR ('Administrators <%s>'%to_addr) # msg[‘To‘]
receives a string instead of a list, and if there are multiple email addresses, it ,
can be separated by msg['Subject'] = Header ('Greetings from SMTP ...','Utf-8'). Encode () server= Smtplib. SMTP (Smtp_server, 25) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) Server.quit ()
More content
smtplib模块:
Responsible for sending mail
To read a message:
ImportPoplib#Enter email address, password and POP3 server address:email = input ('Email:') Password= Input ('Password:') Pop3_server= Input ('POP3 Server:')#to connect to the POP3 server:Server =Poplib. POP3 (Pop3_server)#You can turn debug information on or off:Server.set_debuglevel (1)#Optional: Print welcome text for POP3 Server:Print(Server.getwelcome (). Decode ('Utf-8'))#Identity authentication:server.user (email) server.pass_ (password)#stat () returns the number of messages and space occupied:Print('Messages:%s. Size:%s'%Server.stat ())#list () returns the number of all messages:RESP, mails, octets =server.list ()#You can view the returned list similar to [b ' 1 82923 ', B ' 2 2184 ', ...]Print(mails)#to get the latest message, note that the index number starts at 1:index =len (mails) RESP, lines, octets=SERVER.RETR (Index)#lines stores each line of the original text of the message,#you can get the original text for the entire message:Msg_content = b'\ r \ n'. Join (lines). Decode ('Utf-8')#resolve the message later:msg =Parser (). PARSESTR (msg_content)#You can delete messages directly from the server based on the message index number:#Server.dele (Index)#To close the connection:Server.quit ()
More content
2.
http protocol: The transport protocol between the browser and the server
get/http/1.1
Opens a Web page GET request that /
represents the path to the URL, HTTP/1.1
indicating that the HTTP protocol version used is 1.1
Domain name:
200
Represents a successful response, followed by a OK
description. The failed response is 404 Not Found
: The webpage does not exist: there is an 500 Internal Server Error
error inside the server:
http/1.1 OK
Content-Type
Indicates the content of the response, here is the text/html
HTML page that represents:
Content-type:text/html
HTTP Request:
Method get: Request resource only
Method Post: The request resource also comes with user data
Path:/full/url/path
Domain name: specified by the Host header: Host:www.sina.com.cn
Day5-python Study Notes