Skip to content

Commit 4f1680a

Browse files
committed
update
1 parent ff1a342 commit 4f1680a

File tree

8 files changed

+235
-56
lines changed

8 files changed

+235
-56
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
235235

236236
## [LunarSolarConverter](content/LunarSolarConverter.md)
237237

238+
## [chardet](content/chardet.md)
239+
240+
## [cchardet](content/cchardet.md)
241+
238242
## [tools](content/tools.md)
239243

240244
## [Other_thing](content/other_thing.md)

code/cchardet_demo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# coding=utf-8
2+
3+
import cchardet
4+
5+
source = u'你好,世界'
6+
utf8_source = source.encode('utf-8')
7+
gbk_source = source.encode('gbk')
8+
9+
print cchardet.detect(utf8_source)
10+
print cchardet.detect(gbk_source)
11+
12+
print utf8_source.decode(cchardet.detect(utf8_source)['encoding'])
13+
print utf8_source.decode(cchardet.detect(gbk_source)['encoding'])

code/chardet_demo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# coding=utf-8
2+
3+
import chardet
4+
5+
source = u'你好,世界'
6+
utf8_source = source.encode('utf-8')
7+
gbk_source = source.encode('gbk')
8+
9+
print chardet.detect(utf8_source)
10+
print chardet.detect(gbk_source)
11+
12+
print utf8_source.decode(chardet.detect(utf8_source)['encoding'])
13+
print utf8_source.decode(chardet.detect(gbk_source)['encoding'])

code/mailcontrol.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33
import smtplib
44
from email.mime.text import MIMEText
55

6+
7+
def send_mail(username, password, receive, subject, content, host="smtp.163.com", ssl=True):
8+
a = MailControl(username=username, password=password, host=host, ssl=ssl)
9+
a.setSubject(subject)
10+
a.setReceiver(receive)
11+
a.setContent(content)
12+
a.send()
13+
14+
615
class MailControl(object):
716
"""docstring for MailControl"""
817

9-
def __init__(self, username, password, host="smtp.qq.com"):
18+
def __init__(self, username, password, host="smtp.163.com", ssl=True):
19+
"""
20+
import mail_control
21+
a = mail_control.MailControl(username="[email protected]",
22+
password="XXXXXX")
23+
a.setSubject("How Are You?")
24+
a.setReceiver("[email protected]")
25+
a.setContent("这是我给你的爱")
26+
a.send()
27+
"""
1028
self.host = host
1129
self.username = username
1230
self.password = password
1331
self.subject = None
1432
self.receive = None
1533
self.message = None
1634
self.smtpObj = smtplib.SMTP(self.host)
17-
self.smtpObj.starttls()
35+
if ssl:
36+
self.smtpObj.starttls()
1837
try:
1938
self.smtpObj.login(self.username, self.password)
2039
except smtplib.SMTPException as e:
@@ -26,7 +45,6 @@ def __del__(self):
2645
self.smtpObj.close()
2746
except smtplib.SMTPException as e:
2847
print(tuple(e))
29-
pass
3048

3149
def setSubject(self, subject):
3250
self.subject = subject
@@ -53,17 +71,11 @@ def send(self):
5371
except smtplib.SMTPException as e:
5472
print(tuple(e))
5573

56-
"""
57-
58-
import mailcontrol
59-
60-
a = mailcontrol.MailControl(username="[email protected]",password="XXXXXX")
61-
62-
a.setSubject("How Are You?")
63-
64-
a.setReceive("[email protected]")
6574

66-
a.setContent("这是我给你的信")
75+
if __name__ == '__main__':
6776

68-
a.send()
69-
"""
77+
a = MailControl(username="[email protected]", password="XXXXXX")
78+
a.setSubject("How Are You?")
79+
a.setReceiver("[email protected]")
80+
a.setContent("这是我给你的爱")
81+
a.send()

content/cchardet.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## cchardet
2+
3+
c 语言写的字符串编码识别库,更快更准确
4+
5+
```
6+
# coding=utf-8
7+
8+
import cchardet
9+
10+
source = u'你好,世界'
11+
utf8_source = source.encode('utf-8')
12+
gbk_source = source.encode('gbk')
13+
14+
print cchardet.detect(utf8_source)
15+
print cchardet.detect(gbk_source)
16+
17+
print utf8_source.decode(cchardet.detect(utf8_source)['encoding'])
18+
print utf8_source.decode(cchardet.detect(gbk_source)['encoding'])
19+
20+
```

content/chardet.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## chardet
2+
3+
识别字符串编码类型,还能返回识别的准确率
4+
5+
```
6+
# coding=utf-8
7+
8+
import chardet
9+
10+
source = u'中国'
11+
utf8_source = source.encode('utf-8')
12+
gbk_source = source.encode('gbk')
13+
14+
print chardet.detect(utf8_source)
15+
print chardet.detect(gbk_source)
16+
17+
```

content/smtplib.md

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ print "Sending Successful"
4848
smtpObj.close()
4949
```
5050

51-
保存为qqmail_smtp_demo.py,运行,看一下结果。
51+
保存为qqmail_smtp_demo.py,运行,看一下结果。
5252

53-
![qqmail_smtp_demo.jpg](images/qqmail_smtp_demo.jpg)
53+
![qqmail_smtp_demo.jpg](images/qqmail_smtp_demo.jpg)
5454

55-
这样就可以发送一个简单的邮件了,其实真正核心的代码只有四行。
55+
这样就可以发送一个简单的邮件了,其实真正核心的代码只有四行。
5656

57-
1. smtplib.SMTP( [host [, port [, local_hostname]]] )
58-
这句代码用来创建一个SMTP对象,并连接邮件服务器。
57+
1. smtplib.SMTP( [host [, port [, local_hostname]]] )
58+
这句代码用来创建一个SMTP对象,并连接邮件服务器。
5959
>在此处,也可以化为两步。<br>
6060
>先创建对象`smtpObj = smtplib.SMTP()`,再连接服务器 `smtpObj.connect(host)`
6161
2. SMTP.login(user,password)
@@ -95,9 +95,9 @@ smtpObj.close()
9595

9696
![163mail_smtp_demo.jpg](images/163mail_smtp_demo.jpg)
9797

98-
##### 发送HTML格式的邮件
98+
##### 发送HTML格式的邮件
9999

100-
其实发送HTML格式的邮件也非常简单,在MIMETest对象里面指定文本为HTML即可。
100+
其实发送HTML格式的邮件也非常简单,在MIMETest对象里面指定文本为HTML即可。
101101

102102
```python
103103

@@ -135,7 +135,7 @@ smtpObj.close()
135135

136136
##### 发送附件
137137

138-
发送附件则需要创建 MIMEMultipart 实例,然后构造附件发送。
138+
发送附件则需要创建 MIMEMultipart 实例,然后构造附件发送。
139139

140140
```python
141141

@@ -228,9 +228,9 @@ print "Sending Successful"
228228
smtpObj.close()
229229
```
230230

231-
保存为163mail_smtp_image.py,运行,看一下结果。
231+
保存为163mail_smtp_image.py,运行,看一下结果。
232232

233-
![163mail_smtp_image](images/163mail_smtp_image.png)
233+
![163mail_smtp_image](images/163mail_smtp_image.png)
234234

235235
###### SSL与TLS
236236

@@ -283,67 +283,86 @@ smtpObj.close()
283283
###### 一个常用的 邮件类
284284

285285
```
286-
286+
#coding=utf-8
287287
288288
import smtplib
289289
from email.mime.text import MIMEText
290290
291+
292+
def send_mail(username, password, receive, subject, content, host="smtp.163.com", ssl=True):
293+
a = MailControl(username=username, password=password, host=host, ssl=ssl)
294+
a.setSubject(subject)
295+
a.setReceiver(receive)
296+
a.setContent(content)
297+
a.send()
298+
299+
291300
class MailControl(object):
292301
"""docstring for MailControl"""
293-
def __init__(self,username,password,host="smtp.163.com"):
302+
303+
def __init__(self, username, password, host="smtp.163.com", ssl=True):
304+
"""
305+
import mail_control
306+
a = mail_control.MailControl(username="[email protected]",
307+
password="XXXXXX")
308+
a.setSubject("How Are You?")
309+
a.setReceiver("[email protected]")
310+
a.setContent("这是我给你的爱")
311+
a.send()
312+
"""
294313
self.host = host
295314
self.username = username
296315
self.password = password
316+
self.subject = None
317+
self.receive = None
318+
self.message = None
297319
self.smtpObj = smtplib.SMTP(self.host)
298-
self.smtpObj.starttls()
320+
if ssl:
321+
self.smtpObj.starttls()
299322
try:
300-
self.smtpObj.login(self.username,self.password)
301-
except smtplib.e:
302-
print e.args[1]
323+
self.smtpObj.login(self.username, self.password)
324+
except smtplib.SMTPException as e:
325+
print(tuple(e))
326+
exit(0)
303327
304328
def __del__(self):
305329
try:
306330
self.smtpObj.close()
307-
except:
308-
pass
331+
except smtplib.SMTPException as e:
332+
print(tuple(e))
309333
310-
def setSubject(self,subject):
334+
def setSubject(self, subject):
311335
self.subject = subject
312-
if hasattr(self,"message"):
336+
if self.message:
313337
self.message["Subject"] = self.subject
314338
315-
def setReceiver(self,receive):
339+
def setReceiver(self, receive):
316340
self.receive = receive
317-
if hasattr(self,"message"):
341+
if self.message:
318342
self.message["To"] = self.receive
319343
320-
def setContent(self,content,_type="plain",charset="utf-8"):
321-
self.message = MIMEText(content,_subtype=_type,_charset=charset)
344+
def setContent(self, content, _type="plain", charset="utf-8"):
345+
self.message = MIMEText(content, _subtype=_type, _charset=charset)
322346
self.message["From"] = self.username
323-
if hasattr(self,"subject"):
347+
if self.subject:
324348
self.message["Subject"] = self.subject
325-
if hasattr(self,"receive"):
349+
if self.receive:
326350
self.message["To"] = self.receive
327351
328352
def send(self):
329353
try:
330-
self.smtpObj.sendmail(self.username,self.receive,self.message.as_string())
331-
except Exception,e:
332-
print e
354+
self.smtpObj.sendmail(self.username, self.receive,
355+
self.message.as_string())
356+
except smtplib.SMTPException as e:
357+
print(tuple(e))
333358
334-
"""
335359
336-
import mailcontrol
360+
if __name__ == '__main__':
337361
338-
a = mailcontrol.MailControl(username="[email protected]",password="XXXXXX")
339-
340-
a.setSubject("How Are You?")
341-
342-
a.setReceiver("[email protected]")
343-
344-
a.setContent("这是我给你的信")
345-
346-
a.send()
347-
"""
362+
a = MailControl(username="[email protected]", password="XXXXXX")
363+
a.setSubject("How Are You?")
364+
a.setReceiver("[email protected]")
365+
a.setContent("这是我给你的爱")
366+
a.send()
348367
349368
```

0 commit comments

Comments
 (0)