Python3正则表达式
正则表达式是用于匹配字符串中字符组合的模式。在Python中,我们可以使用re模块来使用正则表达式。
re模块
Python的re模块提供了正则表达式的支持。该模块内置了一些函数,例如search()、findall()等等。
我们可以通过导入re模块来使用这些函数,例如:
import re
re.match()
re.match()函数尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()函数就返回None。
import re
pattern = r"hello"
string = "hello, world!"
match = re.match(pattern, string)
print(match)
上述代码输出:
<re.Match object; span=(0, 5), match='hello'>
re.search()
re.search()函数会在字符串内查找模式匹配,只要找到第一个匹配然后返回,如果字符串没有匹配,则返回None。
import re
pattern = r"world"
string = "hello, world!"
search = re.search(pattern, string)
print(search)
上述代码输出:
<re.Match object; span=(7, 12), match='world'>
re.findall()
re.findall()函数会以列表形式返回字符串中所有匹配的模式。
import re
pattern = r"i"
string = "hello, world!"
findall = re.findall(pattern, string)
print(findall)
上述代码输出:
['i', 'i']
re.sub()
re.sub()函数会替换字符串中的匹配项。
import re
pattern = r"world"
string = "hello, world!"
sub = re.sub(pattern, "there", string)
print(sub)
上述代码输出:
hello, there!
re.compile()
re.compile()函数可以将正则字符串编译成正则表达式对象,以便在后面的匹配中复用。
import re
pattern = r"hello"
string = "hello, world!"
compiled_pattern = re.compile(pattern)
match = compiled_pattern.match(string)
print(match)
上述代码输出:
<re.Match object; span=(0, 5), match='hello'>
Python3 CGI编程
CGI(Common Gateway Interface)是一种通用的网关接口,它允许web服务器获取客户端发送的数据并将其传递给后端程序进行处理。Python3语言可以通过cgi模块来实现CGI编程,该模块提供了一些函数用于处理HTTP请求和响应。
Python3 CGI编程示例
以下是一个简单的Python3 CGI编程示例,它创建了一个HTML表单,该表单可以接收用户输入,将其传递给CGI程序进行处理,并将结果返回给用户。
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import cgi
# 创建FieldStorage实例化
form = cgi.FieldStorage()
# 获取输入字段的值
message = form.getvalue("message", "Hello!")
# 设置HTTP响应头
print("Content-type:text/html")
print()
# 生成HTML响应
print("<html>")
print("<head>")
print("<title>Python3 CGI编程示例</title>")
print("</head>")
print("<body>")
print("<h2>Python3 CGI编程示例</h2>")
print("<form action='/cgi-bin/hello.py' method='post'>")
print("<p>消息: <input type='text' name='message' value='%s'></p>" % message)
print("<input type='submit' value='提交'>")
print("</form>")
print("<hr>")
print("<p>输入的消息是: %s</p>" % message)
print("</body>")
print("</html>")
上述代码将生成一个HTML表单,该表单可以接收用户输入并将其传递给CGI程序进行处理。在本例中,CGI程序将简单地返回用户输入的消息。
Python3提供了丰富的网络编程库,它可以用于构建各种类型的网络应用程序。让我们看一下它提供了哪些功能:
socket模块:提供了底层的网络编程API,包括UDP和TCP套接字。
select模块:提供了一种处理多个套接字的方法,可以使一个进程同时处理多个套接字。
asyncore模块:提供了一种异步的套接字处理方式。
asynchat模块:提供了异步的套接字处理方式,并且具有更高层次的抽象,可以用于实现基于协议的网络应用程序。
下面是一个简单的Python3网络编程示例,该示例演示了如何使用socket模块创建一个TCP服务器:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import socket
# 创建一个TCP/IP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定套接字到本地地址和端口
server_address = ('localhost', 8080)
server_socket.bind(server_address)
# 开始监听传入的连接
server_socket.listen(5)
while True:
# 等待连接
print('等待连接...')
client_socket, client_address = server_socket.accept()
try:
print('连接来自:', client_address)
# 接收数据并发送回客户端
while True:
data = client_socket.recv(1024)
print('接收到: ', data)
if data:
print('发送回客户端...')
client_socket.sendall(data)
else:
print('没有数据了')
break
finally:
# 关闭套接字连接
client_socket.close()
上述代码创建了一个TCP服务器,它绑定到本地地址和端口,然后开始监听传入的连接。当客户端连接时,服务器接收数据并将其发送回客户端。在本例中,服务器只是简单地回显客户端发送的数据。
除了socket模块之外,Python3还提供了其他模块来实现网络编程。例如,使用urllib模块可以轻松地从Web服务器下载内容,使用http模块可以实现HTTP客户端和服务器。
Python3 SMTP发送邮件及使用
Python3中的smtplib模块提供了一种用于发送电子邮件的简单方式。它允许您连接到SMTP服务器并发送邮件。在本文中,我们将了解如何使用Python3的smtplib模块发送电子邮件。
SMTP简介
SMTP代表Simple Mail Transfer Protocol(简单邮件传输协议),是用于发送和接收电子邮件的标准协议。SMTP服务器是专门用于发送邮件的计算机程序,它接受来自发送方的电子邮件,并将其传递到接收方。
Python3 SMTP模块
Python3的smtplib模块提供了SMTP客户端实现。使用它,我们可以连接到SMTP服务器并发送电子邮件。
以下是一个简单的Python3程序,该程序使用smtplib模块向一个或多个收件人发送电子邮件:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# 设置电子邮件地址和密码
email = "your_email@example.com"
password = "your_password"
# 设置收件人和发送者
recipient = "recipient@example.com"
sender = "sender@example.com"
# 创建电子邮件消息
message = MIMEText("Python3 SMTP发送邮件示例")
message['From'] = sender
message['To'] = recipient
message['Subject'] = "Python3 SMTP发送邮件示例"
# 连接SMTP服务器并发送电子邮件
with smtplib.SMTP_SSL('smtp.example.com', 465) as server:
server.login(email, password)
server.sendmail(sender, recipient, message.as_string())
print("邮件发送成功!")
上述代码使用smtplib模块连接到SMTP服务器。它使用MIMEText类创建了一个电子邮件消息,并使用SMTP服务器的sendmail()函数将电子邮件消息发送给指定的收件人。
连接SMTP服务器
要使用smtplib模块发送电子邮件,您需要连接到SMTP服务器。您可以使用SMTP类来连接到SMTP服务器。例如,以下代码片段演示如何连接到SMTP服务器:
import smtplib
# 创建SMTP类的实例并连接到SMTP服务器
with smtplib.SMTP_SSL('smtp.example.com', 465) as server:
server.login("your_email@example.com", "your_password")
上述代码使用SMTP_SSL()函数创建SMTP类的实例,并使用指定的SMTP服务器和端口号连接到SMTP服务器。它还使用login()函数登录到SMTP服务器。在本例中,我们使用了SMTP的SSL加密连接。
发送电子邮件
要发送电子邮件,您需要使用SMTP类的sendmail()函数。该函数接受三个参数:sender(发送者的电子邮件地址),recipients(收件人的电子邮件地址)和message(电子邮件消息)。
以下代码演示如何使用SMTP类发送电子邮件:
import smtplib
from email.mime.text import MIMEText
# 创建电子邮件消息
message = MIMEText("Python3 SMTP发送邮件示例")
message['From'] = "sender@example.com"
message['To'] = "recipient@example.com"
message['Subject'] = "Python3 SMTP发送邮件示例"
# 连接SMTP服务器并发送电子邮件
with smtplib.SMTP_SSL('smtp.example.com', 465) as server:
server.login("your_email@example.com", "your_password")
server.sendmail("sender@example.com", "recipient@example.com", message.as_string())
上述代码使用MIMEText类创建了一个电子邮件消息。然后,它使用SMTP服务器的sendmail()函数将电子邮件消息发送给指定的收件人。
总结
Python3的smtplib模块提供了SMTP客户端实现,用于连接到SMTP服务器并发送电子邮件。使用MIMEText类,我们可以创建电子邮件消息。然后,我们可以使用SMTP服务器的sendmail()函数将电子邮件消息发送给指定的收件人。