2018年6月5日笔记

  • socketserver例题
 1 # test_server.py
 2 # server端代码
 3 
 4 import socketserver
 5 
 6 class MyTCPHandle(socketserver.BaseRequestHandler):
 7     def handle(self):
 8         self.data = self.request.recv(1024).strip()
 9         print("{0}:{1} wrote:".format(self.client_address[0], self.client_address[1]))
10         print(self.data)
11         self.request.sendall(self.data.upper())
12 
13 if __name__ == '__main__':
14     host, port = 'localhost', 9999
15     address = (host, port)
16     server = socketserver.TCPServer(address, MyTCPHandle)
17     server.serve_forever()
 1 # test_client.py
 2 # client端代码
 3 
 4 import socket
 5 import socketserver
 6 
 7 host, port = "localhost", 9999
 8 data = "Good Evening"
 9 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10 try:
11     sock.connect((host, port))
12     sock.sendall(data.encode("utf-8"))
13     recv_data = sock.recv(1024)
14 except Exception as e:
15     raise e
16 finally:
17     sock.close()
18 
19 print("sent: {0}".format(data))
20 print("recv: {0}".format(recv_data))
# 执行test_server.py和test_clent.py

# client端结果如下:
sent: Good Evening
recv: b'GOOD EVENING'

# server端结果如下:
127.0.0.1:49467 wrote:
b'hello world'
127.0.0.1:49475 wrote:
b'Good Evening'

 

 

  • http.server例题
 1 '''
 2 python -m http.server 80    (python3)
 3 python -m SimpleHTTPRequest     (python2)
 4 '''
 5 
 6 
 7 import http.server
 8 import socketserver
 9 port = 8000
10 host = '127.0.0.1'
11 address = (host, port)
12 # handle = SimpleHTTPRequest.SimpleHTTPRequestHandler
13 handle = http.server.SimpleHTTPRequestHandler
14 with socketserver.TCPServer(address, handle) as httpd:
15     print("server start")
16     httpd.serve_forever()
server start

 

 

  • 发邮件之yagmail模块
 1 import yagmail
 2 
 3 
 4 args={
 5     "user": "luyi20130820@126.com",
 6     "password": "xxx",
 7     "host": "smtp.126.com",
 8     "port": "465"
 9 }
10 
11 
12 emailList=["1106438132@qq.com","karlkiller@icloud.com"]
13 email = yagmail.SMTP(**args)
14 email.send(to='1106438132@qq.com',subject="原因分析",contents='''DT:SPM 是出错信息的关键词,可以在这个网页中找到出错的原因http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html,原因:550 DT:SPM 邮件正文带有很多垃圾邮件特征或发送环境缺乏规范性。需调整邮件内容或优化发送环境;原因分析邮件中带有敏感关键词,例如促销,发票等。邮件中包含超级链接,或者超级链接太多。垃圾邮件特征比较明显,例如:只有一张图片,或只有一张图片。发送相同的邮件内容太多了。处理这种情况的方法是:换其他邮箱发送,或调整邮件内容。''',cc="karlkiller@icloud.com")

 

 

  • 发邮件之smtplib模块
 1 import email.mime.multipart
 2 import email.mime.text
 3 import smtplib
 4 
 5 
 6 
 7 msg = email.mime.multipart.MIMEMultipart()
 8 msg['from'] = '1106438132@qq.com'
 9 msg['to'] =  'karlkiller@icloud.com'
10 msg['subject'] = 'It is time to go bed'
11 
12 context = '''
13     <h1>晚上好</h1>
14     你好,
15      这是一封骚扰邮件。
16       www.ustchacker.com hello
17     '''
18 text = email.mime.text.MIMEText(_text=context, _subtype="html")
19 msg.attach(text)
20 
21 em = smtplib.SMTP_SSL()
22 em.connect("smtp.qq.com", 465)
23 em.login("1106438132@qq.com", 'xxxxx')
24 em.sendmail(from_addr='1106438132@qq.com', to_addrs='karlkiller@icloud.com', msg=msg.as_string())
25 em.quit()

 

posted on 2018-06-07 00:17  Karlkiller  阅读(207)  评论(0编辑  收藏  举报

导航