File: httpget.py

package info (click to toggle)
python-greenio 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 144 kB
  • ctags: 200
  • sloc: python: 988; makefile: 32
file content (39 lines) | stat: -rw-r--r-- 736 bytes parent folder | download
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
##
# Copyright (c) 2013 Yury Selivanov
# License: Apache 2.0
##


import greenio
import asyncio


@asyncio.coroutine
def sleeper():
    while True:
        yield from asyncio.sleep(0.05)
        print('.')


@greenio.task
def get():
    from greenio import socket

    sock = socket.create_connection(('python.org', 80))
    print('connected', sock._sock)
    sock.sendall(b'GET / HTTP/1.0\r\n\r\n')
    print('sent')
    print('rcvd', sock.recv(1024))
    sock.close()


@asyncio.coroutine
def run():
    yield from asyncio.wait(
        [get(), sleeper()], return_when=asyncio.FIRST_COMPLETED)


asyncio.set_event_loop_policy(greenio.GreenEventLoopPolicy())
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()