Twisted Challenges

Challenge 1: Identity Server and Client

This challenge tests your knowledge on the concepts of Transport, Protocol and ProtocolFactory in Twisted.

In this challenge, we have a server and a client that are based the Echo server/client in Twisted official documentation. Instead of sending the request back and forth between the peers, the server responds with a string of the client’s IP and port (identity).

Q1: Echo Client’s IP and Port as Response

  • Make proper changes to implement the following:
    • Client Accepts user inputs from stdin and sends the inputs with <enter> key
    • Server responds with a message saying You are from <ip> on port <port>, saying "bla bla bla"
    • Then, server immediately loses the connection after the response

Q2: Display Connection History

  • Based on Q1, make further changes to implement:
    • Record a history of IP and port of connected clients.
    • When client sends a message show log, respond a minified json structured as below
        "ip": "",
        "port": 60000
    ... ,
    ... ,
    ... ,
        "ip": "",
        "port": 60001

Sample Answer:

Challenge 2: Deferred 101

This challenge tests the basics of Deferred objects.

In this challenge, we have a simple synchronous HTTP client that needs refactoring. Your challenge is to change this code with Twisted to implement the features below.

Q1: Fetch the webpage

  • Read function synchronousCall
  • Implement a function called chainedCall as an asynchronous version with Deferred
  • Pass the test case testChainedCall

Q2: Wrap your call chains with inlineCallbacks

  • Based on the previous question, wrap the function chainedCall with inlineCallbacks
  • Pass the test case testInlineCallback

Sample Answer:

Challenge 3: Message Relay

This challenge tests your knowledge on chained calls with callback and errBack.

In this challenge, we have 3 persons relaying a message. Each person can either pass(callback) or blame(errback) to the next person. The behaviour of each person’s pass/blame are pre-defined as functions in code.

Q1: Add code in function q1 below to stop Person 3 blaming

Q2: Make the message pass through Person 2 without Person 1 blaming

Sample Answer

Challenge 4: Heartbeat

This challenge tests your knowledge on loopingCall.

The existing code is to send out heartbeat signal of “*” every 3 seconds to each session after connection. Note: this is not a broadcast which sends the “*” to all clients at the same time.

To test your heartbeat server, use netcat or telnet, connect to to port 5000.

Sample Answer

Challenge 5: Who Got “Yes” First

This challenge tests your knowledge on DeferredList, Failure and your comprehensive applied knowledge of Twisted.

There is a web JSON API return randomly-picked answer of either yes or no. The challenge code is a client that sends 3 requests to the API.

Q1: Add code to function getAll to retrieve all responses and then print the collected results with pprint (which is provided)

Q2: Change code to function _filterAnswer and function _handleUnexpectedAnswer to get the first answer “yes” and stop waiting for all other requests. Do not interrupt if there is no answer “yes” in any response in which case (None, 0) will be printed.

When you are making your change, please make sure the following is satisfied:

  • _filterAnswer filters out “yes” to the callback chain while it raises an UnexpectedAnswerError to error-back chain if it’s a “No”.
  • _handleUnexpectedAnswer handles all failures from _filerAnswer. It catches and stops the complaint due to UnexpectedAnswerError and puts the Deferred object back to callback chain. But it still raises any other types of failures.

Q3: Add code to function _displayUnexpectedAnswer to print the filtered-out response(s) of “No” using print.

Sample Answer

Challenge 6: Concurrency Limit

This challenge tests your knowledge on DeferredSemaphore.

There is a script that gets the file sizes of gcc releases. Please add code function download to limit concurrent requests sent by Deferred objects by maxRun.

Sample Answer

Challenge 7: Cancel on Timeout

This challenge tests your knowledge on cancellation and timeout.

The existing code needs a line of code to set a timeout of 2 seconds before cancel the deferred if timeout is reached. On timeout, please use logTimeout when it is timed out.

Sample Answer

Challenge 8: Chain Them Up

This challenge tests your knowledge on chaining deferreds.

The original code implements a callback chain. Please refactor the two lines of addCallbacks as an oneliner with chainDeferred.

Sample Answer

Challenge 9: Work with Synchronous Functions

This challenge tests your knowledge on integrating synchronous functions.

The original code implements a Deferred object from a function. please reactor the function to return a Deffered.

Sample Answer