15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

calling the write() method of the transport object to send another message to the server,<br />

loseConnection() is executed, closing the socket. When this occurs, the factory's clientConnection-Lost<br />

() method will be called, and our reactor is stopped, completing execution of our script. We also stop<br />

the reactor if a clientConnection-Failed() for some other reason.<br />

The final part of the script is where we create a client factory and make a connection to the server and<br />

run the reactor. Note that we instantiate the client factory here instead of passing it in to the reactor like<br />

in the server. This is because we are not the server waiting for clients to talk to us, and its factory<br />

makes a new protocol object for each connection. We are one client, so we make a single protocol object<br />

that connects to the server whose factory makes one to talk to ours.<br />

16.5.3. Executing Our TCP Server and Client(s)<br />

The Twisted client displays output similar to all our other clients:<br />

$ tsTclntTW.py<br />

> Where is hope<br />

...sending Where is hope...<br />

[Tue Apr 18 23:53:09 2006] Where is hope<br />

> When words fail<br />

...sending When words fail...<br />

[Tue Apr 18 23:53:14 2006] When words fail<br />

><br />

$<br />

The server is back to a single connection. Twisted maintains the connection and does not close the<br />

transport after every message:<br />

$ tsTservTW.py<br />

waiting for connection...<br />

...connected from: 127.0.0.1<br />

The "connection from" output does not have the other information because we only asked for the host/<br />

address from the getPeer() method of the server's transport object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!