[Pkg-privacy-commits] [pyptlib] 26/136: Migrating client and server examples to pyptlib.easy
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:02 UTC 2015
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository pyptlib.
commit 5297c0922730cf27afe01625cabdb5b2c63e3fc5
Author: Brandon Wiley <brandon at blanu.net>
Date: Fri Jun 29 12:01:26 2012 -0500
Migrating client and server examples to pyptlib.easy
---
src/examples/client.py | 85 ++++++++++++++++++++++++++++----------------------
src/examples/daemon.py | 41 ------------------------
src/examples/server.py | 71 ++++++++++++++++++++---------------------
src/pyptlib/client.py | 14 ++++-----
4 files changed, 89 insertions(+), 122 deletions(-)
diff --git a/src/examples/client.py b/src/examples/client.py
index 2a418dd..621168e 100755
--- a/src/examples/client.py
+++ b/src/examples/client.py
@@ -8,42 +8,53 @@ import argparse
from struct import unpack
from socket import inet_ntoa
-from pyptlib.config.config import EnvException
-from pyptlib.config.client import ClientConfig
-
-class ManagedClient(Daemon):
- def __init__(self):
- try:
- Daemon.__init__(self, ClientConfig(), SocksHandler())
- except EnvException:
- print('error 0')
- return
- except UnsupportedManagedTransportVersionException:
- print('error 1')
- return
- except NoSupportedTransportsException:
- print('error 2')
- return
-
- try:
- self.launchClient(self.supportedTransport, 8182)
- self.config.writeMethod(self.supportedTransport, 5, ('127.0.0.1', 8182), None, None)
- except TransportLaunchException as e:
- print('error 3')
- self.config.writeMethodError(self.supportedTransport, e.message)
-
- self.config.writeMethodEnd()
-
- self.run()
-
- def launchClient(self, name, port):
- if name!=self.supportedTransport:
- raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
-
- client=DummyClient()
- self.handler.setTransport(client)
- add_service(Service(self.handler, port=port))
+from pyptlib.config import EnvException
+from pyptlib.client import ClientConfig
+
+class UnsupportedManagedTransportVersionException(Exception):
+ pass
+
+class NoSupportedTransportsException(Exception):
+ pass
+
+class TransportLaunchException(Exception):
+ def __init__(self, message):
+ message=message
+
+def launchClient(self, name, port):
+ if name!=supportedTransport:
+ raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
if __name__=='__main__':
- server=ManagedClient()
-
+ supportedTransportVersion='1'
+ supportedTransport='dummy'
+ config=ClientConfig()
+
+ if config.checkManagedTransportVersion(supportedTransportVersion):
+ config.writeVersion(supportedTransportVersion)
+ else:
+ config.writeVersionError()
+ raise UnsupportedManagedTransportVersionException()
+
+ if not config.checkTransportEnabled(supportedTransport):
+ raise NoSupportedTransportsException()
+ except EnvException:
+ print('error 0')
+ return
+ except UnsupportedManagedTransportVersionException:
+ print('error 1')
+ return
+ except NoSupportedTransportsException:
+ print('error 2')
+ return
+
+ try:
+ launchClient(supportedTransport, 8182)
+ config.writeMethod(supportedTransport, 5, ('127.0.0.1', 8182), None, None)
+ except TransportLaunchException as e:
+ print('error 3')
+ config.writeMethodError(supportedTransport, e.message)
+
+ config.writeMethodEnd()
+
+ run()
diff --git a/src/examples/daemon.py b/src/examples/daemon.py
deleted file mode 100644
index c365b1b..0000000
--- a/src/examples/daemon.py
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import time
-
-from struct import unpack
-from socket import inet_ntoa
-
-class Daemon:
- config=None
- handler=None
-
- supportedTransportVersion='1'
- supportedTransport='dummy'
-
- def __init__(self, configManager, handler):
- self.config=configManager
- self.handler=handler
-
- if self.config.checkManagedTransportVersion(self.supportedTransportVersion):
- self.config.writeVersion(self.supportedTransportVersion)
- else:
- self.config.writeVersionError()
- raise UnsupportedManagedTransportVersionException()
-
- if not self.config.checkTransportEnabled(self.supportedTransport):
- raise NoSupportedTransportsException()
-
- def run(self):
- eventloop.run()
-
-class UnsupportedManagedTransportVersionException(Exception):
- pass
-
-class NoSupportedTransportsException(Exception):
- pass
-
-class TransportLaunchException(Exception):
- def __init__(self, message):
- self.message=message
-
diff --git a/src/examples/server.py b/src/examples/server.py
index b8515b3..d70c354 100755
--- a/src/examples/server.py
+++ b/src/examples/server.py
@@ -6,42 +6,39 @@ import sys
from struct import unpack
from socket import inet_ntoa
-from pyptlib.config.config import EnvException
-from pyptlib.config.server import ServerConfig, MethodOptions
-
-class ManagedServer(Daemon):
- def __init__(self):
- try:
- Daemon.__init__(self, ServerConfig(), ProxyHandler())
- except EnvException:
- print('error 0')
- return
- except UnsupportedManagedTransportVersionException:
- print('error 1')
- return
- except NoSupportedTransportsException:
- print('error 2')
- return
-
- try:
- self.launchServer(self.supportedTransport, 8182)
- self.config.writeMethod(self.supportedTransport, ('127.0.0.1', 8182), MethodOptions())
- except TransportLaunchException as e:
- print('error 3')
- self.config.writeMethodError(self.supportedTransport, e.message)
-
- self.config.writeMethodEnd()
-
- self.run()
-
- def launchServer(self, name, port):
- if name!=self.supportedTransport:
- raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
-
- client=DummyServer()
- self.handler.setTransport(client)
- add_service(Service(self.handler, port=port))
+from pyptlib.config import EnvException
+from pyptlib.server import ServerConfig, MethodOptions
+
+supportedTransport='dummy'
+
+def launchServer(self, name, port):
+ if name!=supportedTransport:
+ raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
if __name__=='__main__':
- server=ManagedServer()
-
+ supportedTransportVersion='1'
+ config=ClientConfig()
+ try:
+ if config.checkManagedTransportVersion(supportedTransportVersion):
+ config.writeVersion(supportedTransportVersion)
+ else:
+ config.writeVersionError()
+ raise UnsupportedManagedTransportVersionException()
+ except EnvException:
+ print('error 0')
+ return
+ except UnsupportedManagedTransportVersionException:
+ print('error 1')
+ return
+ except NoSupportedTransportsException:
+ print('error 2')
+ return
+
+ try:
+ launchServer(supportedTransport, 8182)
+ config.writeMethod(supportedTransport, ('127.0.0.1', 8182), MethodOptions())
+ except TransportLaunchException as e:
+ print('error 3')
+ config.writeMethodError(supportedTransport, e.message)
+
+ config.writeMethodEnd()
diff --git a/src/pyptlib/client.py b/src/pyptlib/client.py
index 0cc268c..2b58a6e 100644
--- a/src/pyptlib/client.py
+++ b/src/pyptlib/client.py
@@ -8,16 +8,16 @@ Configuration for a Pluggable Transport client.
__docformat__ = 'restructuredtext'
-class ClientConfig(Config):
+class ClientConfig(Config):
#Public methods
-
+
def __init__(self): # throws EnvError
Config.__init__(self)
-
+
self.transports=self.get('TOR_PT_CLIENT_TRANSPORTS').split(',')
if '*' in self.transports:
self.allTransportsEnabled=True
- self.transports.remove('*')
+ self.transports.remove('*')
# Returns a list of strings representing the client transports reported by Tor. If present, '*' is stripped from this list and used to set allTransportsEnabled to True.
def getClientTransports(self):
@@ -31,13 +31,13 @@ class ClientConfig(Config):
methodLine=methodLine+' ARGS='+args.join(',')
if optArgs and len(optArgs)>0:
methodLine=methodLine+' OPT-ARGS='+args.join(',')
- print(methodLine)
-
+ print(methodLine)
+
# Write a message to stdout specifying that an error occurred setting up the specified method
# Takes: str, str
def writeMethodError(self, name, message): # CMETHOD-ERROR
print('CMETHOD-ERROR %s %s' % (name, message))
-
+
# Write a message to stdout specifying that the list of supported transports has ended
def writeMethodEnd(self): # CMETHODS DONE
print('CMETHODS DONE')
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git
More information about the Pkg-privacy-commits
mailing list