GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python_test/test55.py at master · sinferwu/python_test

Expand file tree

/

test55.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
52 lines (40 loc) · 1.39 KB
/

test55.py

Copy path

File metadata and controls

52 lines (40 loc) · 1.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr.bin.env python 3
# -*- coding: utf-8 -*-
'''
练习python的async/await
async:相当于asyncio.coroutine,可以用这个替换这个更加方便
await:相当于yield form,把这个改成await即可替代
以上都是在python3.5版本以上才有的
'''
__author__ = 'sergiojune'
import asyncio
async def hello(): # 这样就是相当于一个coroutine了
print('hello world')
await asyncio.sleep(1)
print('hello again')
# loop = asyncio.get_event_loop()
# loop.run_until_complete(hello())
# loop.close()
# 两个协程
# loop = asyncio.get_event_loop()
# loop.run_until_complete(asyncio.wait([hello(), hello()]))
# loop.close()
# 再用这个方法连接搜狐新浪和网易
async def wegt(host):
print('wegt %s ' % host)
# 连接
connect = asyncio.open_connection(host, 80)
reader, writer = await connect
header = 'GET/HTTP/1.0\r\nHost:%s\r\n\r\n' % host
writer.write(header.encode('utf-8'))
await writer.drain() # 相当于刷新缓存
while True:
line = await reader.readline()
if line == b'\r\n':
print()
break
print('%s header > %s' % (host, line.decode('utf-8').strip()))
connect.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([wegt(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]))
loop.close()
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.