summaryrefslogtreecommitdiff
path: root/pypers/marelli/modulo4/remote_tester_client.py
blob: b991019d959968f8ad73a769b445fc0a899d7be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Read code from a file, send it to the server and display the result.

usage: %prog testfile [options]

"""

import os, sys

from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor, protocol

from remote_tester_server import PORT

class ClientFactory(protocol.ClientFactory):
    class protocol(LineReceiver):
        def connectionMade(self):
            print "Sending data to server ..."
            send(self.factory.name_code, self.transport)
        def connectionLost(self, arg):
            print "Connection lost!", arg
        def lineReceived(self, line):
            print line
            if line == "bye":
                pass
                #self.transport.loseConnection("regular exit")
                #reactor.stop()
    def __init__(self, name, lines):
        self.name_code = name, lines
            
def send((name, rfile), transport):
    transport.write("BEGIN %s\r\n" % name)
    for line in rfile:
        transport.write(line.rstrip() + "\r\n")
    transport.write("END\r\n") 

def send_and_wait(fname, lines):
    reactor.connectTCP("localhost", PORT, ClientFactory(fname, lines))
    reactor.run()

if __name__ == "__main__":
    try:
        testfile = sys.argv[1]
    except IndexError:
        print __doc__
    else:
        send_and_wait(os.path.basename(testfile), file(testfile, "U"))