github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

husio / corrosion

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Python light thread toy framework — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

README update 
Piotr HusiatyƄski (author)
Sun Oct 11 02:04:00 -0700 2009
commit  f67391f0afd6d3e07c325d7a6c2871fa15b9605f
tree    361e0e17ff462131e0684fd62f781f512d11f4cb
parent  f56c28fdca220132e27d9a57e38c8048435928d4
corrosion / src / examples / example_1.py src/examples/example_1.py
100644 56 lines (45 sloc) 1.554 kb
edit raw blame history
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
53
54
55
56
# -*- coding: utf-8 -*-
 
import socket
import optparse
 
from corrosion.core.scheduler import Scheduler
from corrosion.core import calls
from corrosion.net.asocket import Socket
 
 
def handle_request(sock, addr):
    data = ''
    while True:
        data += yield sock.recv(1024)
        # using telnet for tests..
        if '\r\n' in data:
            break
    response = 'echo: ' + data
    yield sock.send(response)
    yield sock.close()
 
def echo(raw_socket, host, port):
    sock = Socket(raw_socket)
    sock.bind((host, port))
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.listen(1)
    while True:
        print '>> waiting for new connection'
        conn, addr = yield sock.accept()
        # spawn new light-thread task
        yield calls.NewTask(handle_request(conn, addr))
 
 
 
def main():
    parser = optparse.OptionParser('usage: %prog [-p <port>] [-h <host>]')
    parser.add_option('-p', '--port', type='int', default=8080, dest='port',
            help='server port')
    parser.add_option('--host', default='', dest='host',
            help='server host')
    (options, args) = parser.parse_args()
 
    scheduler = Scheduler()
    raw_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    scheduler.add(echo(raw_socket, options.host, options.port))
    print('>> server running %s:%d' % (options.host or '0.0.0.0', options.port))
    try:
        scheduler.run()
    except KeyboardInterrupt:
        scheduler.stop()
    finally:
        raw_socket.close()
 
if __name__ == '__main__':
    main()
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server