Skip to content
This repository
Newer
Older
100644 128 lines (71 sloc) 4.125 kb
5eb134a1 »
2011-06-14 initial commit; 0.0.
1 # TCP
2
3
4 <span id="TcpClient"/>
5 ## thor.TcpClient ( _loop_ )
6
7 A TCP client. _loop_ is a *thor.loop*; if omitted, the "default" loop will be used.
8
9 Note that new connections will not emit *data* events until they are unpaused; see [thor.tcp.TcpConnection.pause](#pause).
10
11 For example:
12
68c6a6c1 »
2011-11-02 clean up tcp client example
13 import sys
14 import thor
15
16 test_host, test_port = sys.argv[1:2]
17
5eb134a1 »
2011-06-14 initial commit; 0.0.
18 def handle_connect(conn):
19 conn.on('data', sys.stdout.write)
68c6a6c1 »
2011-11-02 clean up tcp client example
20 conn.on('close', thor.stop)
21 conn.write("GET /\n\n")
5eb134a1 »
2011-06-14 initial commit; 0.0.
22 conn.pause(False)
68c6a6c1 »
2011-11-02 clean up tcp client example
23
24 def handle_err(err_type, err):
25 sys.stderr.write(str(err_type))
26 thor.stop()
5eb134a1 »
2011-06-14 initial commit; 0.0.
27
68c6a6c1 »
2011-11-02 clean up tcp client example
28 c = thor.TcpClient()
29 c.on('connect', handle_connect)
5eb134a1 »
2011-06-14 initial commit; 0.0.
30 c.on('connect_error', handle_err)
31 c.connect(test_host, test_port)
68c6a6c1 »
2011-11-02 clean up tcp client example
32 thor.run()
5eb134a1 »
2011-06-14 initial commit; 0.0.
33
34
35 <span id="client_connect"/>
36 ### thor.TcpClient.connect ( _host_, _port_, _timeout_ )
37
38 Call to initiate a connection to _port_ on _host_. [connect](#client_connect_event) will be emitted when a connection is available, and [connect_error](#connect_error) will be emitted when it fails.
39
40 If _timeout_ is given, it specifies a connect timeout, in seconds. If the timeout is exceeded and no connection or explicit failure is encountered, [connect_error](#connect_error) will be emitted with *socket.error* as the _errtype_ and *errno.ETIMEDOUT* as the _error_.
41
42
43 <span id="client_connect_event"/>
44 ### event 'connect' ( _connection_ )
45
46 Emitted when the connection has succeeded. _connection_ is a [TcpConnection](#TcpConnection).
47
48
49 <span id="connect_error"/>
50 ### event 'connect\_error' ( _errtype_, _error_ )
51
52 Emitted when the connection failed. _errtype_ is *socket.error* or *socket.gaierror*; _error_ is the error type specific to the type.
53
54
55 <span id="TcpServer"/>
56 ## thor.TcpServer ( _host_, _port_, _loop_ )
57
58 A TCP server. _host_ and _port_ specify the host and port to listen on, respectively; if given, _loop_ specifies the *thor.loop* to use. If _loop_ is omitted, the "default" loop will be used.
59
60 Note that new connections will not emit *data* events until they are unpaused; see [thor.tcp.TcpConnection.pause](#pause).
61
62 For example:
63
64 s = TcpServer("localhost", 8000)
65 s.on('connect', handle_conn)
66
70db547d »
2012-02-28 Add start and stop events to servers.
67 <span id="server_start_event"/>
68 ### event 'start'
69
70 Emitted when the server starts.
5eb134a1 »
2011-06-14 initial commit; 0.0.
71
72 <span id="server_connect_event"/>
73 ### event 'connect' ( _connection_ )
74
75 Emitted when a new connection is accepted by the server. _connection_ is a [TcpConnection](#TcpConnection).
76
77 ### thor.TcpServer.close () <span id="server_close"/>
78
79 Stops the server from accepting new connections.
80
70db547d »
2012-02-28 Add start and stop events to servers.
81 <span id="server_stop_event"/>
82 ### event 'stop'
83
84 Emitted when the server stops.
85
5eb134a1 »
2011-06-14 initial commit; 0.0.
86
87 <span id="TcpConnection"/>
88 ## thor.tcp.TcpConnection ()
89
90 A single TCP connection.
91
92
93 <span id="data_event"/>
94 ### event 'data' ( _data_ )
95
96 Emitted when incoming _data_ is received by the connection. See [thor.tcp.TcpConnection.pause](#pause) to control these events.
97
98
99 ### event 'close' () <span id="close_event"/>
100
101 Emitted when the connection is closed, either because the other side has closed it, or because of a network problem.
102
103
104 ### event 'pause' ( _paused_ ) <span id="pause_event"/>
105
106 Emitted to indicate the pause state, using _paused_, of the outgoing side of the connection (i.e., the *write* side).
107
108 When True, the connection buffers are full, and *write* should not be called again until this event is emitted again with _paused_ as False.
109
110
111 <span id="write"/>
112 ### thor.tcp.TcpConnection.write ( _data_ )
113
114 Write _data_ to the connection. Note that it may not be sent immediately.
115
116
117 <span id="pause"/>
118 ### thor.tcp.TcpConnnection.pause ( _paused_ )
119
120 Controls the incoming side of the connection (i.e., *data* events). When _paused_ is True, incoming [data](#data_event) events will stop; when _paused_ is false, they will resume again.
121
122 Note that by default, *TcpConnection*s are paused; i.e., to read from them, you must first *thor.tcp.TcpConnection.pause*(_False_).
123
124
125 <span id="close"/>
126 ### thor.tcp.TcpConnection.close ()
127
128 Close the connection. If there is data still in the outgoing buffer, it will be written before the socket is shut down.
Something went wrong with that request. Please try again.