用python写代码,将本机ip发送到指定邮箱,比如abc@abc.com
时间: 2023-07-16 18:15:18 浏览: 90
好的,以下是用Python发送本机IP地址到指定邮箱的代码:
```python
import socket
import smtplib
from email.mime.text import MIMEText
# 获取本机IP地址
ip = socket.gethostbyname(socket.gethostname())
# 邮件发送设置
from_addr = 'your-email-address'
password = 'your-email-password'
to_addr = 'abc@abc.com'
smtp_server = 'smtp.your-email-provider.com'
# 构造邮件内容
msg = MIMEText(ip, 'plain', 'utf-8')
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'IP地址通知'
# 发送邮件
server = smtplib.SMTP(smtp_server, 587)
server.starttls()
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
```
注意替换代码中的邮件发送设置和IP地址获取方式。如果您的邮箱提供商需要开启SMTP服务,请自行前往邮箱设置开启。
相关问题
请给我10个python语言下的正则表达式实例代码
好的,以下是10个Python语言下的正则表达式实例代码:
1. 匹配手机号码
```
import re
phone_number = "13800001111"
pattern = r"^1[3-9]\d{9}$"
if re.match(pattern, phone_number):
print("是合法的手机号码")
else:
print("不是合法的手机号码")
```
2. 匹配邮箱地址
```
import re
email = "abc@xyz.com"
pattern = r"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"
if re.match(pattern, email):
print("是合法的邮箱地址")
else:
print("不是合法的邮箱地址")
```
3. 匹配IP地址
```
import re
ip_address = "192.168.0.1"
pattern = r"^((25[0-5]|2[0-4]\d|[01]?\d?\d)(\.(25[0-5]|2[0-4]\d|[01]?\d?\d)){3})$"
if re.match(pattern, ip_address):
print("是合法的IP地址")
else:
print("不是合法的IP地址")
```
4. 匹配URL地址
```
import re
url = "https://www.google.com"
pattern = r"^(http|https)://[a-zA-Z0-9]+\.[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*(:[0-9]+)?(/[a-zA-Z0-9_-]+)*(\?[a-zA-Z0-9_]+=[a-zA-Z0-9_]+(&[a-zA-Z0-9_]+=[a-zA-Z0-9_]+)*)?$"
if re.match(pattern, url):
print("是合法的URL地址")
else:
print("不是合法的URL地址")
```
5. 匹配身份证号码
```
import re
id_number = "610103199001011234"
pattern = r"^\d{17}(\d|X)$"
if re.match(pattern, id_number):
print("是合法的身份证号码")
else:
print("不是合法的身份证号码")
```
6. 匹配日期格式
```
import re
date = "2022-01-01"
pattern = r"^\d{4}-\d{2}-\d{2}$"
if re.match(pattern, date):
print("是合法的日期格式")
else:
print("不是合法的日期格式")
```
7. 匹配中文字符
```
import re
chinese = "你好,世界!"
pattern = r"[\u4e00-\u9fa5]+"
if re.match(pattern, chinese):
print("是中文字符")
else:
print("不是中文字符")
```
8. 匹配邮政编码
```
import re
postal_code = "100101"
pattern = r"^[0-9]{6}$"
if re.match(pattern, postal_code):
print("是合法的邮政编码")
else:
print("不是合法的邮政编码")
```
9. 匹配QQ号码
```
import re
qq_number = "123456789"
pattern = r"^[1-9][0-9]{4,}$"
if re.match(pattern, qq_number):
print("是合法的QQ号码")
else:
print("不是合法的QQ号码")
```
10. 匹配包含数字和字母的密码
```
import re
password = "Abc123456"
pattern = r"^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
if re.match(pattern, password):
print("是合法的密码")
else:
print("不是合法的密码")
```
请给我30个python语言下的正则表达式的代码实例
好的,以下是30个Python语言下的正则表达式的代码实例:
1. 匹配手机号码
```python
import re
phone_number = "13812345678"
pattern = r"^1[3456789]\d{9}$"
result = re.match(pattern, phone_number)
if result:
print("是手机号码")
else:
print("不是手机号码")
```
2. 匹配电子邮箱地址
```python
import re
email = "example@mail.com"
pattern = r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
result = re.match(pattern, email)
if result:
print("是电子邮箱地址")
else:
print("不是电子邮箱地址")
```
3. 匹配IP地址
```python
import re
ip_address = "192.168.0.1"
pattern = r"^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$"
result = re.match(pattern, ip_address)
if result:
print("是IP地址")
else:
print("不是IP地址")
```
4. 匹配日期格式
```python
import re
date = "2022-01-01"
pattern = r"^\d{4}-\d{2}-\d{2}$"
result = re.match(pattern, date)
if result:
print("是日期格式")
else:
print("不是日期格式")
```
5. 匹配数字
```python
import re
num = "123456"
pattern = r"^\d+$"
result = re.match(pattern, num)
if result:
print("是数字")
else:
print("不是数字")
```
6. 匹配字母
```python
import re
letter = "abcde"
pattern = r"^[a-zA-Z]+$"
result = re.match(pattern, letter)
if result:
print("是字母")
else:
print("不是字母")
```
7. 匹配字母和数字
```python
import re
string = "abc123"
pattern = r"^[a-zA-Z0-9]+$"
result = re.match(pattern, string)
if result:
print("是字母和数字")
else:
print("不是字母和数字")
```
8. 匹配中文
```python
import re
chinese = "中文"
pattern = r"^[\u4e00-\u9fa5]+$"
result = re.match(pattern, chinese)
if result:
print("是中文")
else:
print("不是中文")
```
9. 匹配用户名
```python
import re
username = "user_123"
pattern = r"^[a-zA-Z][a-zA-Z0-9_]{5,17}$"
result = re.match(pattern, username)
if result:
print("是用户名")
else:
print("不是用户名")
```
10. 匹配密码
```python
import re
password = "123456"
pattern = r"^[a-zA-Z]\w{5,17}$"
result = re.match(pattern, password)
if result:
print("是密码")
else:
print("不是密码")
```
11. 匹配URL
```python
import re
url = "https://www.example.com"
pattern = r"^https?://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"
result = re.match(pattern, url)
if result:
print("是URL")
else:
print("不是URL")
```
12. 匹配HTML标签
```python
import re
html = "<div class='example'>Hello, World!</div>"
pattern = r"<[^>]+>"
result = re.findall(pattern, html)
print(result)
```
13. 匹配XML标签
```python
import re
xml = "<example>Hello, World!</example>"
pattern = r"<([a-zA-Z]+)[^>]*>(.*?)</\1>"
result = re.findall(pattern, xml)
print(result)
```
14. 匹配电话号码
```python
import re
phone_number = "010-12345678"
pattern = r"^0\d{2,3}-\d{7,8}$"
result = re.match(pattern, phone_number)
if result:
print("是电话号码")
else:
print("不是电话号码")
```
15. 匹配邮政编码
```python
import re
zipcode = "100000"
pattern = r"^[1-9]\d{5}$"
result = re.match(pattern, zipcode)
if result:
print("是邮政编码")
else:
print("不是邮政编码")
```
16. 匹配身份证号码
```python
import re
id_number = "110101199001010101"
pattern = r"^\d{6}(19|20)\d{2}(0\d|1[0-2])([0-2]\d|3[0-1])\d{3}[0-9Xx]$"
result = re.match(pattern, id_number)
if result:
print("是身份证号码")
else:
print("不是身份证号码")
```
17. 匹配用户名和密码
```python
import re
username = "user_123"
password = "password123"
pattern = r"^[a-zA-Z][a-zA-Z0-9_]{5,17}$|^[a-zA-Z]\w{5,17}$"
result1 = re.match(pattern, username)
result2 = re.match(pattern, password)
if result1 and result2:
print("是用户名和密码")
else:
print("不是用户名和密码")
```
18. 匹配QQ号码
```python
import re
qq_number = "123456789"
pattern = r"^[1-9]\d{4,10}$"
result = re.match(pattern, qq_number)
if result:
print("是QQ号码")
else:
print("不是QQ号码")
```
19. 匹配微信号
```python
import re
wechat_id = "abc123"
pattern = r"^[a-zA-Z][-_a-zA-Z0-9]{5,19}$"
result = re.match(pattern, wechat_id)
if result:
print("是微信号")
else:
print("不是微信号")
```
20. 匹配车牌号
```python
import re
license_plate = "京A12345"
pattern = r"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}\w{5}$"
result = re.match(pattern, license_plate)
if result:
print("是车牌号")
else:
print("不是车牌号")
```
21. 匹配微博账号
```python
import re
weibo_id = "@example"
pattern = r"^@[\w\u4e00-\u9fa5]+"
result = re.match(pattern, weibo_id)
if result:
print("是微博账号")
else:
print("不是微博账号")
```
22. 匹配身高
```python
import re
height = "180cm"
pattern = r"^\d{2,3}cm$"
result = re.match(pattern, height)
if result:
print("是身高")
else:
print("不是身高")
```
23. 匹配体重
```python
import re
weight = "60kg"
pattern = r"^\d{2,3}kg$"
result = re.match(pattern, weight)
if result:
print("是体重")
else:
print("不是体重")
```
24. 匹配IPV6地址
```python
import re
ipv6_address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
pattern = r"^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"
result = re.match(pattern, ipv6_address)
if result:
print("是IPV6地址")
else:
print("不是IPV6地址")
```
25. 匹配MAC地址
```python
import re
mac_address = "00:11:22:33:44:55"
pattern = r"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"
result = re.match(pattern, mac_address)
if result:
print("是MAC地址")
else:
print("不是MAC地址")
```
26. 匹配XML注释
```python
import re
xml_comment = "<!-- This is a comment -->"
pattern = r"<!--[\s\S]*?-->"
result = re.findall(pattern, xml_comment)
print(result)
```
27. 匹配HTML标签属性
```python
import re
html_tag = "<div class='example'>Hello, World!</div>"
pattern = r"([\w-]+)\s*=\s*\"([^\"]+)\""
result = re.findall(pattern, html_tag)
print(result)
```
28. 匹配RGB颜色值
```python
import re
rgb_color = "rgb(255, 255, 255)"
pattern = r"^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$"
result = re.match(pattern, rgb_color)
if result:
print("是RGB颜色值")
else:
print("不是RGB颜色值")
```
29. 匹配HSL颜色值
```python
import re
hsl_color = "hsl(0, 0%, 100%)"
pattern = r"^hsl\((\d{1,3}),\s*(\d{1,3})%,\s*(\d{1,3})%\)$"
result = re.match(pattern, hsl_color)
if result:
print("是HSL颜色值")
else:
print("不是HSL颜色值")
```
30. 匹配十六进制颜色值
```python
import re
hex_color = "#ffffff"
pattern = r"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
result = re.match(pattern, hex_color)
if result:
print("是十六进制颜色值")
else:
print("不是十六进制颜色值")
```
阅读全文