Skip to content

Commit cacea33

Browse files
author
gongjd
committed
增加例子
1 parent 7619994 commit cacea33

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

mytest/SMTP.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
import smtplib
5+
from email.mime.text import MIMEText
6+
from email.utils import formataddr
7+
8+
my_sender = '429240967@qq.com' # 发件人邮箱账号
9+
my_pass = 'xxxxxxxxxx' # 发件人邮箱密码
10+
my_user = '429240967@qq.com' # 收件人邮箱账号,我这边发送给自己
11+
12+
13+
def mail():
14+
ret = True
15+
try:
16+
msg = MIMEText('填写邮件内容', 'plain', 'utf-8')
17+
msg['From'] = formataddr(["FromRunoob", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
18+
msg['To'] = formataddr(["FK", my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
19+
msg['Subject'] = "菜鸟教程发送邮件测试" # 邮件的主题,也可以说是标题
20+
21+
server = smtplib.SMTP("smtp.qq.com", 25) # 发件人邮箱中的SMTP服务器,端口是25
22+
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
23+
server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
24+
server.quit() # 关闭连接
25+
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
26+
ret = False
27+
return ret
28+
29+
30+
ret = mail()
31+
if ret:
32+
print("邮件发送成功")
33+
else:
34+
print("邮件发送失败")

mytest/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
# 文件名:client.py
4+
5+
import socket # 导入 socket 模块
6+
7+
s = socket.socket() # 创建 socket 对象
8+
host = socket.gethostname() # 获取本地主机名
9+
port = 12345 # 设置端口好
10+
11+
s.connect((host, port))
12+
print s.recv(1024)
13+
s.close()

mytest/server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
# 文件名:server.py
4+
5+
import socket # 导入 socket 模块
6+
7+
s = socket.socket() # 创建 socket 对象
8+
host = socket.gethostname() # 获取本地主机名
9+
port = 12345 # 设置端口
10+
s.bind((host, port)) # 绑定端口
11+
12+
s.listen(5) # 等待客户端连接
13+
while True:
14+
c, addr = s.accept() # 建立客户端连接。
15+
print '连接地址:', addr
16+
c.send('欢迎访问菜鸟教程!')
17+
c.close() # 关闭连接

mytest/thread.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
import thread
5+
import time
6+
7+
8+
# 为线程定义一个函数
9+
def print_time(threadName, delay):
10+
count = 0
11+
while count < 5:
12+
time.sleep(delay)
13+
count += 1
14+
print "%s: %s" % (threadName, time.ctime(time.time()))
15+
16+
17+
# 创建两个线程
18+
try:
19+
thread.start_new_thread(print_time, ("Thread-1", 2,))
20+
thread.start_new_thread(print_time, ("Thread-2", 4,))
21+
except:
22+
print "Error: unable to start thread"
23+
24+
while 1:
25+
pass

0 commit comments

Comments
 (0)