async def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/hello/{name}', hello) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8080) log_time_data('Server started at port 8080 ...') return srv
def handle(): r = '' while True: n = yield r if not n: return log_time_data('recv: %s' % n) r = '200 ok'
def sender(handle): handle.send(None) n = 0 while n < 5: n += 1 log_time_data('send: %s' % n) r = handle.send(n) log_time_data('return: %s' % r) handle.close()
def wget(host): log_time_data('wget: %s' % host) connect = asyncio.open_connection(host, 80) reader, writer = yield from connect header = 'GET / HTTP/1.0\r\nHost:%s\r\n\r\n' % host writer.write(header.encode('utf-8')) yield from writer.drain() while True: line = yield from reader.readline() if line == b'\r\n': break log_time_data('%s header > %s' % (host, line.decode('utf-8').rstrip())) writer.close()
def email_send(send_addr, passwd, recv_addr, smtp_server, port): log_time_data('confirm send_addr: %s' % send_addr) log_time_data('confirm passwd: %s' % passwd) log_time_data('confirm recv_addr: %s' % recv_addr) log_time_data('confirm smtp_server: %s' % smtp_server) log_time_data('confirm port: %s' % port) msg = MIMEMultipart() msg['From'] = _format_addr('Sender<%s>' % send_addr) msg['To'] = _format_addr('Recever<%s>' % recv_addr) msg['Subject'] = Header('Python send Email', 'utf-8').encode() msgText = MIMEText('Send From Python... (text)', 'plain', 'utf-8') msg.attach(msgText) msgHtml = MIMEText( '<html><body><h1>Python</h1><p>Send From Python... (HTML)<img src="cid:0"></p></body></html>', 'html', 'utf-8') msg.attach(msgHtml) with open('./res/zf.jpeg', 'rb') as f: mime = MIMEBase('image', 'jpeg', filename='zf.jpeg') mime.add_header('Content-Disposition', 'attachment', filename='zf.jpeg') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) encoders.encode_base64(mime) msg.attach(mime) server = smtplib.SMTP(smtp_server, port) server.starttls() server.set_debuglevel(1) server.login(send_addr, passwd) server.sendmail(send_addr, [recv_addr], msg.as_string()) server.quit()
def blurImg(fileName): filepath, name, suffix = handlefilePath(fileName) im = Image.open(fileName) log_time_data('open img: %s' % fileName) im = im.filter(ImageFilter.BLUR) log_time_data('img blur') fileName_ = filepath + name + '_' + suffix im.save(fileName_, 'png') log_time_data('save img: %s' % fileName_)
async def async_await(last): log_time_data('Hello 1 last: %s' % last) #使用await代替yield await asyncio.sleep(1) log_time_data('Hello 2 last: %s' % last)
def hello(last): log_time_data('Hello 1 last: %s' % last) r = yield from asyncio.sleep(last) log_time_data('Hello 2 last: %s' % last)
def wsgi(): server = make_server('', 8080, appication) log_time_data('Http listening on port 8080 ...') server.serve_forever()