tcp_client.py

"""Connecct to a TCP server, in this case a web server.
   Request HTML. Split it up by words 
   and randomly place it on the screen.
   by stephan)
"""
from slut import *
from random import randint
from slut.net import TcpClient
from twisted.internet.protocol import Protocol
#HINT: you can use any other protocol from twisted
#see http://twistedmatrix.com/


class Atlantis(World):
    def onSetup(self):
        self.name = "RJ45 Plugs Are Awesome"
        self.font = font(12, 'oblivious')
        self.words = []
        TcpClient('cadre.sjsu.edu', 80, StandardClient)

    def onDrawOrtho(self):
        for w in self.words:
            text(w[0], self.font, w[1], w[2])

    def shred(self, text):
        words = text.split(" ")
        self.words = []
        for w in words:
            self.words.append(
                [w, randint(0, 400), randint(12, 400)])



class StandardClient(Protocol):
    def connectionMade(self):
        #write an HTTP request to the server
        self.write("GET /index.html HTTP/1.1
");
        self.write("HOST: cadre.sjsu.edu

");

    def dataReceived(self, data):
        glo.world.shred(data)

    def connectionLost(self, reason):
        print 'connection lost: ', reason

    def write(self, line):
        if self.connected:
            self.transport.write(line)
        else:
            print '-> not connected'


atlantis = Atlantis()
atlantis.run()
Initiated by Stephan Hechenberger
Thanks to CADRE's 103