I want to learn Python secretly, and then shock everyone (day 6)

Insert picture description here

The title is not intended to offend, but I think this ad is fun
. Take the above mind map if you like it. I can’t learn so much anyway.

Preface

Early review: I want to learn Python secretly, and then shock everyone (day 5)

本系列文默认各位有一定的C或C++基础,因为我是学了点C++的皮毛之后入手的Python,这里也要感谢齐锋学长送来的支持。
本系列文默认各位会百度,学习‘模块’这个模块的话,还是建议大家有自己的编辑器和编译器的,上一篇已经给大家做了推荐啦?

我要的不多,点个关注就好啦
然后呢,本系列的目录嘛,说实话我个人比较倾向于那两本 Primer Plus,所以就跟着它们的目录结构吧。

本系列也会着重培养各位的自主动手能力,毕竟我不可能把所有知识点都给你讲到,所以自己解决需求的能力就尤为重要,所以我在文中埋得坑请不要把它们看成坑,那是我留给你们的锻炼机会,请各显神通,自行解决。

Okay, this is a "playful essay". See how many fun projects we will do today? Sit down, open your compiler, whether it is an online compiler or a PC compiler

When this is over, I will start crawling when I write an article.


Mass mailing

A friend asked me to write a mass mailing function, then come? (Here manually fill up the emoji package)

I have been learning for so many days. I should be familiar with this method and process. What is the first step?

  1. Find the module or package used for mass mailing
  2. Familiar with or understand the use of the module or package
  3. How to understand? Are there examples in the manual
  4. Revise and modify, such a function will come out

Ok, after clarifying the steps, let's start: Python3.9 library function support


···

Insert picture description here

Insert picture description here


Code reference

Today’s mood is a bit low, just come to a code reference, let’s talk about the code

Pseudo code 1: Send a shell email

Let's take QQ mailbox as an example (because I only have QQ mailbox)

Before doing this, we have to do one thing first: use authorization for third-party clients that open QQ mailboxes.
Our QQ mailbox does not mean that any software can be used to send emails, this requires authorization.
How to do it specifically,

  1. Login QQ mailbox
  2. Settings -> Account -> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service -> activate, activate the first two
  3. It is 2020-10-26. If the interface is revised in the future, please search for the string of English above and keep up with the times

After completing these steps, we will get some authorization codes. Remember to use the latest authorization code.

# smtplib 用于邮件的发信动作
import smtplib	#引入smtplib模块

# 发信方的信息:发信邮箱,QQ邮箱授权码
from_addr = '[email protected]'	
password = '你的授权码'

# 收信方邮箱
to_addr = '[email protected]'

# 发信服务器
smtp_server = 'smtp.qq.com'	#目前就是这个了,你也不用去百度啦,我当时百度了十分钟,最后确定了就是它。。。

# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL()
server.connect(smtp_server,465)	#这个465是端口号,如果不是465就换587试试,实在不行就只能公共端口号25了

# 登录发信邮箱
server.login(from_addr, password)

# 发送邮件
server.sendmail(from_addr, to_addr, msg.as_string())

# 关闭服务器
server.quit()

This is a piece of pseudo-code. Remember, this cannot be run. You need to fill in the missing information. Of course, we are not in a hurry to run it until I finish.

Pseudocode 2: Building the email content

The above pseudo-code is the shell of the email, and the specific content of the email must be realized by the email package

Insert picture description here

Here we will introduce the import statement again.

For email thispackage
There is a difference between the import of this package and the import of modules. I remember that I used import to import which module and it didn’t work (you don’t need to look back, I am amnesia, I don’t know if it was sent), and there are results now. Up.

This is about the difference between "module" and "package". A module (module) is generally a file, and a package (package) is a directory. A package can contain many modules. It can be said that a package is a "module package". "consist of.

Where you can't see, there is an init.py file that secretly controls all this (of course, there are ways to see it), and init.py controls the import behavior of the package. If this file is empty, then we can do nothing if we just import the package. Therefore, importing email directly does not work.

Therefore, we need to use the from… import… statement to import the [needed object] from [a file] in the email package directory. For example, introduce the MIMEText method from the text file in the email package.

MIMEText(msg,type,chartset)
# msg:文本内容,可自定义
# type:文本类型,默认为plain(纯文本)
# chartset:文本编码,中文为“utf-8”

Step here, in fact, it would be able to issue a message of (in front of the pseudo-code portions will not fill msg found on this: msg = MIMEText('send by python','plain','utf-8'))

Friends with strong hands-on skills can try it by themselves, and friends with weak hands-on skills can follow me down.
You will find that this is a headless email.

The message header (header, yes it is also called header) is this area, including subject, sender, recipient and other information:

from email.header import Header

msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header('python test')

Add this piece to your code, the effect is as follows:

from email.mime.text import MIMEText
from email.header import Header
import smtplib

#这里的邮箱请自己填哦
msg = MIMEText('猜猜我是谁:send by python','plain','utf-8')
smtp_server = 'smtp.qq.com'
from_addr = '[email protected]'
to_addr = '[email protected]'
#to_addr = '[email protected]'

msg['From'] = Header('小锋')
msg['To'] = Header(to_addr)

msg['Subject'] = Header('这是一份Python发送的邮件哦,今天刚学的哈哈哈')


server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)

#server = smtplib.SMTP()
#server.connect(smtp_server,25)

server.login(from_addr, 'XXX')#授权码要选最新的
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()

Fill in the parameters in this code, and then you can send it.


group email

For group posting, there are two ways, I will talk about one, mention one, and leave one:

The first method is to write to_addrs as a list:

from email.mime.text import MIMEText
from email.header import Header
import smtplib

msg = MIMEText('猜猜我是谁:send by python','plain','utf-8')
smtp_server = 'smtp.qq.com'
from_addr = '[email protected]'
to_addrs = ['[email protected]','[email protected]','[email protected]']

msg['From'] = Header('小锋')
msg['To'] = Header(",".join(to_addrs))	
#因为server.sendmail(from_addr, to_addrs, msg.as_string())这个函数里面接收的msg参数只能是字符串(不信你把这行去掉试试),所以我们要把这个列表变成字符串

msg['Subject'] = Header('这是一份Python发送的邮件哦,今天刚学的哈哈哈')


server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)

#server = smtplib.SMTP()
#server.connect(smtp_server,25)

server.login(from_addr, '填你自己的')#授权码要选最新的
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()

One mention:
some security optimizations can be done here. Although no one wants to take our code, good habits should be started from an early age.

When I first started to learn programming and did the first project, the teacher told us that the password of this project should be made into cipher text for the user to input, but not to be seen.
So what's wrong in this code?

1. Our accounts are all public (although the above is pseudo-code)
2. Our authorization code is also public
3. Our code is low in reuse and low availability (this involves the second method)

The solution to the first two problems is also very simple, just input.
But what about that list? What should I do?
while loop! !

to_addrs = []
while True:
    a=input('请输入收件人邮箱:')
    #输入收件人邮箱
    to_addrs.append(a)
    #写入列表
    b=input('是否继续输入,n退出,任意键继续:')
    #询问是否继续输入
    if b == 'n':
        break

Improve code reusability and usability

What is code reusability? This is your code. Today I want to send these five people in a group. Yes, tomorrow I want to send the other six people in a group? How to do? Go in and change the code.
If you have to change the code, the reusability design of this code is too bad.

So what is code availability? Are you writing for someone who can write code? That's still the question above. What if she wants to change a few people to post? What should I do if I want to send it to one more person some day, or send it to one less person?

It's not easy to use, she can't.

What to do then?
Remember the modules we used to operate Excel before? Yes, try it yourself.


Confession balloon

The above is more serious. Let everyone do it step by step. Next, let’s take a look at a piece of code to see how much you can understand. If you don’t understand, you can use Baidu. You can also join our small circle to chat with you.

I drew a circle, welcome everyone to our small circle

I have built a Python Q&A group, friends who are interested can find out: What kind of group is this?

Portal through the group: Portal

import turtle
import time

# 画心形圆弧
def chage_angle():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2);

def move_position(x,y):
    turtle.hideturtle() # 隐藏画笔(先)
    turtle.up()  # 提笔
    turtle.goto(x,y)# 移动画笔到指定起始坐标(窗口中心为0,0)
    turtle.down()  # 下笔
    turtle.showturtle()  # 显示画笔

love = input("请输入表白语: ")
signature = input("请输入签名: ")
if love == '':
    love = "I LOVE YOU"


turtle.setup(width=800, height=500)     # 窗口(画布)大小
turtle.color('red', 'pink')     # 画笔颜色
turtle.pensize(3)       # 画笔粗细
turtle.speed(1)     # 描绘速度
# 初始化画笔起始坐标
move_position(x=0,y=-180)   # 移动画笔位置
turtle.left(140)    # 向左旋转140度

turtle.begin_fill()     # 标记背景填充位置

# 画心形直线( 左下方 )
turtle.forward(224)    # 向前移动画笔,长度为224
# 画爱心圆弧
chage_angle()      # 左侧圆弧
turtle.left(120)    # 调整画笔角度
chage_angle()      # 右侧圆弧
# 画心形直线( 右下方 )
turtle.forward(224)

turtle.end_fill()       # 标记背景填充结束位置

# 在心形中写上表白话语
move_position(0,20)      # 表白语位置
turtle.hideturtle()     # 隐藏画笔
turtle.color('#CD5C5C', 'pink')      # 字体颜色
# font:设定字体、尺寸(电脑下存在的字体都可设置)
turtle.write(love, font=('Arial', 30, 'bold'), align='center')

# 签写署名
if signature != '':
    turtle.color('red', 'pink')
    time.sleep(2)
    move_position(180, -180)
    turtle.hideturtle()  # 隐藏画笔
    turtle.write(signature, font=('Arial', 20), align="left")
    
    # 点击窗口关闭程序
window = turtle.Screen()
window.exitonclick()

Let me see if I can add a video
Insert picture description here

That’s no way, it’s not that I don’t add videos

Insert picture description here

You can try it yourself


Long tail flow optimization

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/109282813