diff --git a/core_manual_python.html b/core_manual_python.html index 45c2322..c8d8bc4 100644 --- a/core_manual_python.html +++ b/core_manual_python.html @@ -535,7 +535,7 @@

Registering and Unregistering Ha
from core.event_bus import EventBus
 
 def msg_handler(message):
-print "Got message body %s"% message.body
+    print "Got message body %s"% message.body
 id = EventBus.register_handler('test.address', handler=msg_handler)
 

It's as simple as that. The handler will then receive any messages sent to that address. The object passed into the handler is an instance of class core.Message. The body of the message is available via the body attribute.

@@ -563,15 +563,15 @@

Replying to messages


def msg_handler(message):
     print "I received a message %s"% message.body
 
-# Do some stuff...
-# Now reply to it
-message.reply('This is a reply')
+    # Do some stuff...
+    # Now reply to it
+    message.reply('This is a reply')
 
 EventBus.register_handler('test.address', handler = msg_handler)
 

The sender:

def reply_handler(message):
-print "I received a reply %s"%message.body
+    print "I received a reply %s"%message.body
 
 EventBus.send('test.address', 'This is a message', reply_handler)
 
@@ -767,7 +767,7 @@

One-shot Timers


import vertx
 
 def handler(tid):
-print 'And one second later this is printed'
+    print 'And one second later this is printed'
 
 tid = vertx.set_timer(1000, handler)
 
@@ -861,10 +861,10 @@ 

Closing a Net Server


This function will then be called when the close has fully completed.

def close_handler(err):
 
-if err is None:
+    if err is None:
         print 'Server is closed!'
     else:
-       err.printStackTrace()
+        err.printStackTrace()
 
 server.close(close_handler)
 
@@ -906,8 +906,8 @@

Reading Data from the Socket


@server.connect_handler def connect_handler(sock): -def data_handler(buffer): - print "I received %s bytes of data"% buffer.length + def data_handler(buffer): + print "I received %s bytes of data"% buffer.length sock.data_handler(data_handler) server.listen(1234, 'localhost') @@ -956,8 +956,8 @@

Closed Handler


@server.connect_handler def connect_handler(sock): -def closed_handler(): - print 'The socket is now closed' + def closed_handler(): + print 'The socket is now closed' sock.closed_handler(closed_handler)

The closed handler will be called irrespective of whether the close was initiated by the client or server.

@@ -968,7 +968,7 @@

Exception handler


@server.connect_handler def connect_handler(sock): -@sock.exception_handler + @sock.exception_handler def exception_handler(e): print 'Oops. Something went wrong' @@ -1017,7 +1017,7 @@

Making a Connection


def connect_handler(err, sock): if err is None: - print 'We have connected' + print 'We have connected' client.connect(1234, 'localhost', connect_handler) @@ -1115,7 +1115,7 @@

Flow Control - Streams and Pumps


Flow Control - Streams and Pumps
Flow Control - Streams and Pumps
Flow Control - Streams and Pumps
Request Headers
str = "Headers are\n" @request.headers.each def each(key, value): - str += "#{key}: #{value}\n" + str += "#{key}: #{value}\n" request.response.end(str) @@ -1306,7 +1306,7 @@

Reading Data from the Request BodyTo receive the body, you set the data_handler on the request object. This will then get called every time a chunk of the request body arrives. Here's an example:

@server.request_handler
 def request_handler(request):
-@request.data_handler
+    @request.data_handler
     def data_handler(buffer):
         print "I received %d bytes"% buffer.length
 server.listen(8080, 'localhost')
@@ -1350,7 +1350,7 @@ 

Reading Data from the Request BodyServing files directly from disk

< @server.request_handler def request_handler(request): -file = '' + file = '' if req.path == '/': file = 'index.html' elif '..' not in req.path: @@ -1607,7 +1607,7 @@

Writing Request Headers


def response_handler(resp): print "got response %s"% resp.status_code request = client.post('/some-path', response_handler). -put_header('Some-Header', 'Some-Value'). + put_header('Some-Header', 'Some-Value'). put_header('Some-Other-Header', 'Some-Other-Value'). end()
@@ -1625,8 +1625,8 @@

HTTP Client Responses


client.host = 'foo.com' def response_handler(resp): -print "server returned status code: %s"% resp.status_code -print "server returned status message: %s"% resp.status_message + print "server returned status code: %s"% resp.status_code + print "server returned status message: %s"% resp.status_message client.get_now('/some-path', response_handler) @@ -1639,8 +1639,8 @@

Reading Data from the Response Body def response_handler(resp): @resp.data_handler -def data_handler(buffer): - print "I received %s bytes"% buffer.length + def data_handler(buffer): + print "I received %s bytes"% buffer.length client.get_now('/some-path', response_handler) @@ -1652,17 +1652,17 @@

Reading Data from the Response Body def response_handler(resp): -# Create a buffer to hold the entire response body -body = Buffer.create(0) + # Create a buffer to hold the entire response body + body = Buffer.create(0) -@resp.data_handler -def data_handler(buffer): - # Add chunk to the buffer - body.append_buffer(buffer) + @resp.data_handler + def data_handler(buffer): + # Add chunk to the buffer + body.append_buffer(buffer) -@resp.end_handler -def end_handler(): - # The entire response body has been received + @resp.end_handler + def end_handler(): + # The entire response body has been received print "The total body received was %s bytes"% body.length client.get_now('/some-path', response_handler) @@ -1677,9 +1677,9 @@

Reading Data from the Response Body client.host = 'foo.com' def response_handler(resp): -@resp.body_handler -def body_handler(body): - print "The total body received was %s bytes"% body.length + @resp.body_handler + def body_handler(body): + print "The total body received was %s bytes"% body.length client.get_now('/some-path', response_handler)

Reading cookies


@@ -1695,9 +1695,9 @@

100-Continue Handling


client.host = 'foo.com' def response_handler(resp): -print "Got a response: %s"% resp.status_code -request = client.put('/some-path', response_handler) + print "Got a response: %s"% resp.status_code +request = client.put('/some-path', response_handler) request.put_header('Expect', '100-Continue') request.chunked = True @@ -1765,7 +1765,7 @@

Extracting parameters from the path route_matcher = RouteMatcher() def matched(req): -blogName = req.params['blogname'] + blogName = req.params['blogname'] post = req.params['post'] req.response.end("blogname is %s post is %s" %(blogName, post)) @@ -1867,7 +1867,7 @@

WebSockets on the HTTP client


client.port = 8080 def websocket_handler(websocket): -@websocket.data_handler + @websocket.data_handler def data_handler(buff): print "got %s"% buff websocket.write_text_frame('foo') @@ -1953,9 +1953,9 @@

Reading and writing data config = { 'prefix' : '/echo' } def connect_handler(sock): -@sock.data_handler -def data_handler(buffer): - sock.write(buffer) + @sock.data_handler + def data_handler(buffer): + sock.write(buffer) sockJSServer.install_app(config, connect_hander) httpServer.listen(8080) @@ -2125,7 +2125,7 @@

copy


fs = vertx.file_system() def handler(err, res): -if not err: print 'Copy was successful' + if not err: print 'Copy was successful' fs.copy('foo.dat', 'bar.dat', handler)

Here's an example:

def props_handler(err, props):
-    if err:
+        if err:
         print "Failed to retrieve file props: %s"% err
     else:
         print 'File props are:'
@@ -2199,7 +2199,7 @@ 

read_sym_link(link)

link is the name of the link to read. An usage example would be:

def handler(err, res):
-    if not err: print "Link points at %s"% res
+        if not err: print "Link points at %s"% res
 fs.read_sym_link('somelink', handler)
 

delete


@@ -2225,7 +2225,7 @@

mkdir


If create_parents is true, this creates a new directory and creates any of its parents too. Here's an example

def handler(err, res):
-    if not err: print "Directory created ok"
+        if not err: print "Directory created ok"
 fs.mkdir('a/b/c', True, handler=handler)
 

Here is an example:

def handler(err,res):
-if not err: print "total space: %s"% res.total_space
+    if not err: print "total space: %s"% res.total_space
 fs.fs_props('mydir', handler)
 

open


@@ -2298,11 +2298,11 @@

open


When the file is opened, an instance of AsyncFile is passed into the result handler block:

def handler(err, file):
-if err:
-    print "Failed to open file ", err
-else:
-    print 'File opened ok'
-    file.close()
+    if err:
+        print "Failed to open file ", err
+    else:
+        print 'File opened ok'
+        file.close()
 fs.open('some-file.dat', handler=handler)
 

If read is True, the file will be opened for reading. If write is True the file will be opened for writing. If create_new is True, the file will be created if it doesn't already exist. If flush is True then every write on the file will be automatically flushed (synced) from the OS cache.
@@ -2322,14 +2322,14 @@

Random access writes


Here is an example of random access writes:

def handler(err, async_file):
-if err:
+    if err:
         print "Failed to open file ",err
     else:
         # File open, write a buffer 5 times into a file
         buff = Buffer.create('foo')
         for i in range(1, 6):
-            def write_handler(err, res):          
-                if err:
+                def write_handler(err, res):          
+                    if err:
                     print "Failed to write ", err
                 else:
                     print 'Written ok'  
@@ -2351,13 +2351,13 @@ 

Random access reads


Here's an example of random access reads:

def open_handler(err, async_file):
-if err:
+    if err:
         print "Failed to open file ", err
     else:
         buff = Buffer.create(1000)
         for i in range(1,11):
-            def read_handler(err, res):
-            if err:
+                def read_handler(err, res):
+                if err:
                     print "Failed to read ", err
                 else:
                     print 'Read ok'
@@ -2373,11 +2373,11 @@ 

Using AsyncFile as client.host = 'foo.com' def open_handler(err, async_file): - if err: + if err: print "Failed to open file ", err else: def handler(resp): - print "resp status code %s"% resp.status_code + print "resp status code %s"% resp.status_code request = client.put('/uploads', handler) pump = Pump(async_file, request) @@ -2406,7 +2406,7 @@

lookup


if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup('vertx.io', handler)

@@ -2420,7 +2420,7 @@

lookup_4


if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup_4('vertx.io', handler)
@@ -2434,7 +2434,7 @@

lookup_6


if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup_6('vertx.io', handler)
@@ -2449,7 +2449,7 @@

resolve_a


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_a('vertx.io', handler) @@ -2464,7 +2464,7 @@

resolve_aaaa


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_aaaa('vertx.io', handler) @@ -2479,7 +2479,7 @@

resolve_cname


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_cname('vertx.io', handler) @@ -2493,7 +2493,7 @@

resolve_mx


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_mx('vertx.io', handler) @@ -2513,7 +2513,7 @@

resolve_txt


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_txt('vertx.io', handler) @@ -2527,7 +2527,7 @@

resolve_ns


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_ns('vertx.io', handler) @@ -2541,7 +2541,7 @@

resolve_srv


print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_srv('vertx.io', handler) @@ -2567,7 +2567,7 @@

resolve_ptr


if err: print "Failed to resolve entry ", err else: - println result + print result client.resolve_ptr('1.0.0.10.in-addr.arpa', handler) @@ -2579,7 +2579,7 @@

reverse_lookup


if err: print "Failed to resolve entry ", err else: - println result + print result client.reverse_lookup('10.0.0.1', handler) @@ -2616,20 +2616,20 @@

BADTIME


Bad timestamp

All of those errors are "generated" by the DNS Server itself.

You can obtain the DnsResponseCode from the DnsException like:

-
client = vertx.create_dns_client(('10.0.0.1', 53))
+
import vertx
+import org.vertx.java.core.dns.DnsException
 
 def handler(err, result):
     if err:
-        if err instanceof org.vertx.java.core.dns.DnsException:
-            exception = (org.vertx.java.core.dns.DnsException) err
-            code = exception.code()
+        if isinstance(err, org.vertx.java.core.dns.DnsException):
+            code = err.code()
             ...
-        } else {
+        else:
             print "Failed to resolve entry ", err
-        }
     else:
-        println result
+        print result
 
+client = vertx.create_dns_client(('10.0.0.1', 53))
 client.lookup('nonexisting.vert.io', handler)
 
diff --git a/docs_md/core_manual_python.md b/docs_md/core_manual_python.md index 20b64f9..94dbb4b 100644 --- a/docs_md/core_manual_python.md +++ b/docs_md/core_manual_python.md @@ -76,7 +76,7 @@ The argument to `-conf` is the name of a text file containing a valid JSON objec That configuration is available to the verticle using the `vertx.config()` method. For example: import vertx - + config = vertx.config() # Do something with config @@ -162,7 +162,7 @@ If you have an appplication that is composed of multiple verticles that all need For example, you could create a verticle `app.py` as follows: import vertx - + app_config = vertx.config() # Start the verticles that make up the app @@ -235,7 +235,7 @@ The first argument to the handler is a Java exception if the deployment failed o ## Undeploying a Verticle or Module Any verticles that you deploy programmatically from within a verticle and all of their children are automatically undeployed when the parent verticle is undeployed, so in many cases you will not need to undeploy a verticle manually, however if you do want to do this, it can be done by calling the function `vertx.undeploy_verticle` or `vertx.undeploy_module` passing in the deployment id. - + vertx.undeploy_verticle(deployment_id) You can also provide a handler to the undeploy method if you want to be informed when undeployment is complete. @@ -311,9 +311,9 @@ Let's jump into the API To set a message handler on the address `test.address`, you call the method `register_handler` on the `EventBus` class from core.event_bus import EventBus - + def msg_handler(message): - print "Got message body %s"% message.body + print "Got message body %s"% message.body id = EventBus.register_handler('test.address', handler=msg_handler) It's as simple as that. The handler will then receive any messages sent to that address. The object passed into the handler is an instance of class `core.Message`. The body of the message is available via the `body` attribute. @@ -356,17 +356,17 @@ The receiver: def msg_handler(message): print "I received a message %s"% message.body - - # Do some stuff... - # Now reply to it - message.reply('This is a reply') + + # Do some stuff... + # Now reply to it + message.reply('This is a reply') EventBus.register_handler('test.address', handler = msg_handler) The sender: def reply_handler(message): - print "I received a reply %s"%message.body + print "I received a reply %s"%message.body EventBus.send('test.address', 'This is a message', reply_handler) @@ -464,7 +464,7 @@ A Buffer represents a sequence of zero or more bytes that can be written to or r ## Creating Buffers Create an empty buffer - + from core.buffer import Buffer buff = Buffer.create() @@ -630,9 +630,9 @@ A one shot timer calls an event handler after a certain delay, expressed in mill To set a timer to fire once you use the `vertx.set_timer` function passing in the delay and specifying a handler function which will be called when after the delay: import vertx - + def handler(tid): - print 'And one second later this is printed' + print 'And one second later this is printed' tid = vertx.set_timer(1000, handler) @@ -647,7 +647,7 @@ You can also set a timer to fire periodically by using the `set_periodic` functi import vertx def handler(tid): - print 'And every second this is printed' + print 'And every second this is printed' tid = vertx.set_periodic(1000, handler) @@ -660,9 +660,9 @@ To cancel a timer, call the `cancel_timer` function specifying the timer id. For import vertx def handler(tid): - # This will never be called - pass - + # This will never be called + pass + tid = vertx.set_periodic(1000, handler) # And immediately cancel it @@ -726,14 +726,14 @@ The actual bind is asynchronous so the server might not actually be listening un ### Getting Notified of Incoming Connections To be notified when a connection occurs we need to call the `connect_handler` function of the server, specifying a block which represents the handler. The handler will be called when a connection is made: - + import vertx server = vertx.create_net_server() @server.connect_handler def connect_handler(sock): - print 'A client has connected!' + print 'A client has connected!' server.listen(1234, 'localhost') @@ -751,10 +751,10 @@ This function will then be called when the close has fully completed. def close_handler(err): - if err is None: + if err is None: print 'Server is closed!' else: - err.printStackTrace() + err.printStackTrace() server.close(close_handler) @@ -799,8 +799,8 @@ To read data from the socket you need to set the `data_handler` on the socket. T @server.connect_handler def connect_handler(sock): - def data_handler(buffer): - print "I received %s bytes of data"% buffer.length + def data_handler(buffer): + print "I received %s bytes of data"% buffer.length sock.data_handler(data_handler) server.listen(1234, 'localhost') @@ -838,9 +838,9 @@ Here's an example of a simple TCP echo server which simply writes back (echoes) @server.connect_handler def connect_handler(sock): - def data_handler(buffer): - sock.write(buffer) - sock.data_handler(data_handler) + def data_handler(buffer): + sock.write(buffer) + sock.data_handler(data_handler) server.listen(1234, 'localhost') @@ -863,11 +863,11 @@ If you want to be notified when a socket is closed, you can set the `closed_hand import vertx server = vertx.create_net_server() - + @server.connect_handler def connect_handler(sock): - def closed_handler(): - print 'The socket is now closed' + def closed_handler(): + print 'The socket is now closed' sock.closed_handler(closed_handler) @@ -882,7 +882,7 @@ You can set an exception handler on the socket that will be called if an excepti @server.connect_handler def connect_handler(sock): - @sock.exception_handler + @sock.exception_handler def exception_handler(e): print 'Oops. Something went wrong' @@ -943,9 +943,9 @@ A NetClient is used to make TCP connections to servers. ### Creating a Net Client To create a TCP client we simply create an instance of `core.net.NetClient`. - + import vertx - + client = vertx.create_net_client() ### Making a Connection @@ -955,11 +955,11 @@ To actually connect to a server you invoke the `connect` method import vertx client = vertx.create_net_client() - + def connect_handler(err, sock): if err is None: - print 'We have connected' - + print 'We have connected' + client.connect(1234, 'localhost', connect_handler) The connect method takes the port number as the first parameter, followed by the hostname or ip address of the server. It takes a block as the connect handler. This handler will be called when the connection actually occurs. @@ -1102,7 +1102,7 @@ A naive way to do this would be to directly take the data that's been read and i @server.connect_handler def connect_handler(sock): - @sock.data_handler + @sock.data_handler def data_handler(buffer): # Write the data straight back sock.write(buffer) @@ -1118,10 +1118,10 @@ Since `NetSocket` implements `WriteStream`, we can check if the `WriteStream` is @server.connect_handler def connect_handler(sock): - @sock.data_handler + @sock.data_handler def data_handler(buffer): - if not sock.write_queue_full: - sock.write(buffer) + if not sock.write_queue_full: + sock.write(buffer) server.listen(1234, 'localhost') @@ -1132,7 +1132,7 @@ This example won't run out of RAM but we'll end up losing data if the write queu @server.connect_handler def connect_handler(sock): - @sock.data_handler + @sock.data_handler def data_handler(buffer): sock.write(buffer) if sock.write_queue_full: @@ -1147,14 +1147,14 @@ We're almost there, but not quite. The `NetSocket` now gets paused when the file @server.connect_handler def connect_handler(sock): - @sock.data_handler + @sock.data_handler def data_handler(buffer): sock.write(buffer) if sock.write_queue_full: sock.pause() @sock.drain_handler def drain_handler(): - sock.resume() + sock.resume() server.listen(1234, 'localhost') @@ -1255,7 +1255,7 @@ To be notified when a request arrives you need to set a request handler. This is server = vertx.create_http_server() @server.request_handler def request_handler(request): - print 'An HTTP request has been received' + print 'An HTTP request has been received' server.listen(8080, 'localhost') @@ -1321,7 +1321,7 @@ Here's an example that echoes the headers to the output of the response. Run it str = "Headers are\n" @request.headers.each def each(key, value): - str += "#{key}: #{value}\n" + str += "#{key}: #{value}\n" request.response.end(str) @@ -1356,7 +1356,7 @@ To receive the body, you set the `data_handler` on the request object. This will @server.request_handler def request_handler(request): - @request.data_handler + @request.data_handler def data_handler(buffer): print "I received %d bytes"% buffer.length server.listen(8080, 'localhost') @@ -1373,7 +1373,7 @@ In many cases, you know the body is not large and you just want to receive it in from core.buffer import Buffer server = vertx.create_http_server() - + @server.request_handler def request_handler(request): @@ -1410,7 +1410,7 @@ Here's an example using `body_handler`: @server.request_handler def request_handler(request): - @request.body_handler + @request.body_handler def body_handler(body): print "The total body received was %d bytes"% body.length @@ -1568,7 +1568,7 @@ To do this use the `send_file` function on the HTTP response. Here's a simple HT @server.request_handler def request_handler(request): - file = '' + file = '' if req.path == '/': file = 'index.html' elif '..' not in req.path: @@ -1657,7 +1657,7 @@ To make a request using the client you invoke one the methods named after the HT For example, to make a `POST` request: import vertx - + client = vertx.create_http_client() client.host = 'foo.com' @@ -1683,7 +1683,7 @@ Once you have finished with the request you must call the `end` method. If you don't know the name of the request method in advance there is a general `request` method which takes the HTTP method as a parameter: import vertx - + client = vertx.create_http_client() client.host = 'foo.com' def response_handler(resp): print "got response %s"% resp.status_code @@ -1694,7 +1694,7 @@ If you don't know the name of the request method in advance there is a general ` There is also a method called `get_now` which does the same as `get`, but automatically ends the request. This is useful for simple GETs which don't have a request body: import vertx - + client = vertx.create_http_client() client.host = 'foo.com' @@ -1767,7 +1767,7 @@ You can also use the `put_header` method to enable a more fluent API: def response_handler(resp): print "got response %s"% resp.status_code request = client.post('/some-path', response_handler). - put_header('Some-Header', 'Some-Value'). + put_header('Some-Header', 'Some-Value'). put_header('Some-Other-Header', 'Some-Other-Value'). end() @@ -1793,8 +1793,8 @@ To query the status code of the response use the `status_code` property. The `st client.host = 'foo.com' def response_handler(resp): - print "server returned status code: %s"% resp.status_code - print "server returned status message: %s"% resp.status_message + print "server returned status code: %s"% resp.status_code + print "server returned status message: %s"% resp.status_message client.get_now('/some-path', response_handler) @@ -1811,9 +1811,9 @@ To receive the response body, you set a `data_handler` on the response object wh def response_handler(resp): @resp.data_handler - def data_handler(buffer): - print "I received %s bytes"% buffer.length - + def data_handler(buffer): + print "I received %s bytes"% buffer.length + client.get_now('/some-path', response_handler) The response object implements the `ReadStream` interface so you can pump the response body to a `WriteStream`. @@ -1826,18 +1826,18 @@ As with a server request, if you wanted to read the entire response body before client.host = 'foo.com' def response_handler(resp): - - # Create a buffer to hold the entire response body - body = Buffer.create(0) - - @resp.data_handler - def data_handler(buffer): - # Add chunk to the buffer - body.append_buffer(buffer) - - @resp.end_handler - def end_handler(): - # The entire response body has been received + + # Create a buffer to hold the entire response body + body = Buffer.create(0) + + @resp.data_handler + def data_handler(buffer): + # Add chunk to the buffer + body.append_buffer(buffer) + + @resp.end_handler + def end_handler(): + # The entire response body has been received print "The total body received was %s bytes"% body.length client.get_now('/some-path', response_handler) @@ -1859,9 +1859,9 @@ Here's an example using `body_handler`: client.host = 'foo.com' def response_handler(resp): - @resp.body_handler - def body_handler(body): - print "The total body received was %s bytes"% body.length + @resp.body_handler + def body_handler(body): + print "The total body received was %s bytes"% body.length client.get_now('/some-path', response_handler) #### Reading cookies @@ -1887,12 +1887,12 @@ An example will illustrate this: client.host = 'foo.com' def response_handler(resp): - print "Got a response: %s"% resp.status_code - request = client.put('/some-path', response_handler) + print "Got a response: %s"% resp.status_code + request = client.put('/some-path', response_handler) request.put_header('Expect', '100-Continue') request.chunked = True - + @request.continue_handler def continue_handler(): # OK to send rest of body @@ -1984,7 +1984,7 @@ If you want to extract parameters from the path, you can do this too, by using t route_matcher = RouteMatcher() def matched(req): - blogName = req.params['blogname'] + blogName = req.params['blogname'] post = req.params['post'] req.response.end("blogname is %s post is %s" %(blogName, post)) @@ -2117,9 +2117,9 @@ Here's an example of WebSockets on the client: client = vertx.create_http_client() client.host = "foo.com" client.port = 8080 - + def websocket_handler(websocket): - @websocket.data_handler + @websocket.data_handler def data_handler(buff): print "got %s"% buff websocket.write_text_frame('foo') @@ -2195,7 +2195,7 @@ For example, to create a SockJS echo application: sockJSServer = vertx.create_sockjs_server(httpServer) config = { 'prefix' : '/echo' } - + def connect_handler(sock): Pump(sock, sock).start() @@ -2226,9 +2226,9 @@ The object passed into the SockJS handler implements `ReadStream` and `WriteStre config = { 'prefix' : '/echo' } def connect_handler(sock): - @sock.data_handler - def data_handler(buffer): - sock.write(buffer) + @sock.data_handler + def data_handler(buffer): + sock.write(buffer) sockJSServer.install_app(config, connect_hander) httpServer.listen(8080) @@ -2441,9 +2441,9 @@ Here's an example: import vertx fs = vertx.file_system() - + def handler(err, res): - if not err: print 'Copy was successful' + if not err: print 'Copy was successful' fs.copy('foo.dat', 'bar.dat', handler) @@ -2505,7 +2505,7 @@ Retrieve properties of a file. Here's an example: def props_handler(err, props): - if err: + if err: print "Failed to retrieve file props: %s"% err else: print 'File props are:' @@ -2553,7 +2553,7 @@ Reads a symbolic link. I.e returns the path representing the file that the symbo `link` is the name of the link to read. An usage example would be: def handler(err, res): - if not err: print "Link points at %s"% res + if not err: print "Link points at %s"% res fs.read_sym_link('somelink', handler) ## delete @@ -2585,7 +2585,7 @@ Makes a new empty directory with name `dirname`, and default permissions ` If `create_parents` is `true`, this creates a new directory and creates any of its parents too. Here's an example def handler(err, res): - if not err: print "Directory created ok" + if not err: print "Directory created ok" fs.mkdir('a/b/c', True, handler=handler) * `mkdir(dirname, create_parents, perms)` @@ -2605,13 +2605,13 @@ Lists the contents of a directory * `read_dir(dir_name, filter)` List only the contents of a directory which match the filter. Here's an example which only lists files with an extension `txt` in a directory. - + def handler(err,res): if not err: - print 'Directory contains these .txt files' - @res.each - def each(filename): - print filename + print 'Directory contains these .txt files' + @res.each + def each(filename): + print filename fs.read_dir('mydirectory', '.*\.txt', handler=handler) The filter is a regular expression. @@ -2625,9 +2625,9 @@ Read the entire contents of a file in one go. *Be careful if using this with lar The body of the file will be returned as a `Buffer` in the handler. Here is an example: - + def handler(err,res): - if not err: "File contains: %s bytes"% res.length + if not err: "File contains: %s bytes"% res.length fs.read_file_as_buffer('myfile.dat', handler=handler) ## write_to_file @@ -2649,11 +2649,11 @@ Checks if a file exists. `exists(file)`. Where `file` is the file name. The result is returned in the handler. - + def handler(err,res): - if not err: - if res: print 'exists' - else: print 'does not exist' + if not err: + if res: print 'exists' + else: print 'does not exist' fs.exists('some-file.txt', handler) ## fs_props @@ -2671,7 +2671,7 @@ The result is returned in the handler. The result object has the following field Here is an example: def handler(err,res): - if not err: print "total space: %s"% res.total_space + if not err: print "total space: %s"% res.total_space fs.fs_props('mydir', handler) ## open @@ -2683,11 +2683,11 @@ Opens an asynchronous file for reading \ writing. When the file is opened, an instance of `AsyncFile` is passed into the result handler block: def handler(err, file): - if err: - print "Failed to open file ", err - else: - print 'File opened ok' - file.close() + if err: + print "Failed to open file ", err + else: + print 'File opened ok' + file.close() fs.open('some-file.dat', handler=handler) If `read` is `True`, the file will be opened for reading. If `write` is `True` the file will be opened for writing. If `create_new` is `True`, the file will be created if it doesn't already exist. If `flush` is `True` then every write on the file will be automatically flushed (synced) from the OS cache. @@ -2716,14 +2716,14 @@ The parameters to the method are: Here is an example of random access writes: def handler(err, async_file): - if err: + if err: print "Failed to open file ",err else: # File open, write a buffer 5 times into a file buff = Buffer.create('foo') for i in range(1, 6): - def write_handler(err, res): - if err: + def write_handler(err, res): + if err: print "Failed to write ", err else: print 'Written ok' @@ -2749,13 +2749,13 @@ The parameters to the method are: Here's an example of random access reads: def open_handler(err, async_file): - if err: + if err: print "Failed to open file ", err else: buff = Buffer.create(1000) for i in range(1,11): - def read_handler(err, res): - if err: + def read_handler(err, res): + if err: print "Failed to read ", err else: print 'Read ok' @@ -2776,11 +2776,11 @@ Here's an example of pumping data from a file on a client to a HTTP request: client.host = 'foo.com' def open_handler(err, async_file): - if err: + if err: print "Failed to open file ", err else: def handler(resp): - print "resp status code %s"% resp.status_code + print "resp status code %s"% resp.status_code request = client.put('/uploads', handler) pump = Pump(async_file, request) @@ -2819,7 +2819,7 @@ To lookup the A / AAAA record for "vertx.io" you would typically use it like: if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup('vertx.io', handler) @@ -2838,7 +2838,7 @@ To lookup the A record for "vertx.io" you would typically use it like: if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup_4('vertx.io', handler) @@ -2857,7 +2857,7 @@ To lookup the A record for "vertx.io" you would typically use it like: if err: print "Failed to resolve entry ", err else: - println result + print result client.lookup_6('vertx.io', handler) @@ -2877,7 +2877,7 @@ To lookup all the A records for "vertx.io" you would typically do: print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_a('vertx.io', handler) @@ -2897,7 +2897,7 @@ To lookup all the AAAAA records for "vertx.io" you would typically do: print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_aaaa('vertx.io', handler) @@ -2917,7 +2917,7 @@ To lookup all the CNAME records for "vertx.io" you would typically do: print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_cname('vertx.io', handler) @@ -2935,7 +2935,7 @@ To lookup all the MX records for "vertx.io" you would typically do: print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_mx('vertx.io', handler) @@ -2961,7 +2961,7 @@ To resolve all the TXT records for "vertx.io" you could use something along thes print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_txt('vertx.io', handler) @@ -2978,7 +2978,7 @@ To resolve all the NS records for "vertx.io" you could use something along these print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_ns('vertx.io', handler) @@ -2996,7 +2996,7 @@ To lookup all the SRV records for "vertx.io" you would typically do: print "Failed to resolve entry ", err else: for r in records: - println r + print r client.resolve_srv('vertx.io', handler) @@ -3029,7 +3029,7 @@ To resolve the PTR record for the ipaddress 10.0.0.1 you would use the PTR notio if err: print "Failed to resolve entry ", err else: - println result + print result client.resolve_ptr('1.0.0.10.in-addr.arpa', handler) @@ -3046,7 +3046,7 @@ client = vertx.create_dns_client(('10.0.0.1', 53)) if err: print "Failed to resolve entry ", err else: - println result + print result client.reverse_lookup('10.0.0.1', handler) @@ -3101,21 +3101,20 @@ All of those errors are "generated" by the DNS Server itself. You can obtain the DnsResponseCode from the DnsException like: - - client = vertx.create_dns_client(('10.0.0.1', 53)) + import vertx + import org.vertx.java.core.dns.DnsException def handler(err, result): if err: - if err instanceof org.vertx.java.core.dns.DnsException: - exception = (org.vertx.java.core.dns.DnsException) err - code = exception.code() + if isinstance(err, org.vertx.java.core.dns.DnsException): + code = err.code() ... - } else { + else: print "Failed to resolve entry ", err - } else: - println result + print result + client = vertx.create_dns_client(('10.0.0.1', 53)) client.lookup('nonexisting.vert.io', handler)