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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
# Copyright (c) The PyAMF Project.
# See LICENSE for details.
"""
Python client for socket example.
@since: 0.5
"""
import socket
import pyamf
from server import appPort, host
class AmfSocketClient(object):
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, host, port):
print "Connecting to socket server on %s:%d" % (host, port)
try:
self.sock.connect((host, port))
print "Connected to server.\n"
except socket.error, e:
raise Exception("Can't connect: %s" % e[1])
def start(self):
msg = ''
# tell server we started listening
print "send request: start"
try:
self.sock.send('start')
except socket.error, e:
raise Exception("Can't connect: %s" % e[1])
while len(msg) < 1024:
# read from server
amf = self.sock.recv(1024)
if amf == '':
print "Connection closed."
msg = msg + amf
for obj in pyamf.decode(amf):
print obj
return msg
def stop(self):
print "send request: stop"
self.sock.send('stop')
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p", "--port", default=appPort,
dest="port", help="port number [default: %default]")
parser.add_option("--host", default=host,
dest="host", help="host address [default: %default]")
(options, args) = parser.parse_args()
host = options.host
port = int(options.port)
client = AmfSocketClient()
client.connect(host, port)
try:
client.start()
except KeyboardInterrupt:
client.stop()
|