[Pkg-privacy-commits] [pyptlib] 27/136: Reformatted code with PythonTidy
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:03 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 0abe74b29df5ac70cd5c28e2c44457d2e7b1b4b8
Author: Brandon Wiley <brandon at blanu.net>
Date: Fri Jun 29 13:25:09 2012 -0500
Reformatted code with PythonTidy
---
src/__init__.py | 3 +
src/examples/client.py | 84 +++++++++----------
src/examples/server.py | 69 ++++++++--------
src/pyptlib/__init__.py | 3 +
src/pyptlib/client.py | 65 +++++++++------
src/pyptlib/config.py | 112 ++++++++++++++-----------
src/pyptlib/server.py | 213 +++++++++++++++++++++++++++---------------------
src/pyptlib/util.py | 205 ++++++++++++++++++++++++++--------------------
8 files changed, 425 insertions(+), 329 deletions(-)
diff --git a/src/__init__.py b/src/__init__.py
index e69de29..e0e3e1a 100644
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
diff --git a/src/examples/client.py b/src/examples/client.py
index 621168e..bf2782b 100755
--- a/src/examples/client.py
+++ b/src/examples/client.py
@@ -1,4 +1,5 @@
-#!/usr/bin/env python -u
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
import os
import sys
@@ -11,50 +12,51 @@ from socket import inet_ntoa
from pyptlib.config import EnvException
from pyptlib.client import ClientConfig
+
class UnsupportedManagedTransportVersionException(Exception):
- pass
+
+ pass
+
class NoSupportedTransportsException(Exception):
- pass
+
+ pass
+
class TransportLaunchException(Exception):
- def __init__(self, message):
- message=message
+
+ 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__':
- 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()
+ if name != supportedTransport:
+ raise TransportLaunchException('Tried to launch unsupported transport %s'
+ % name)
+
+
+if __name__ == '__main__':
+ 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()
+
+ try:
+ launchClient(supportedTransport, 8182)
+ config.writeMethod(supportedTransport, 5, ('127.0.0.1', 8182),
+ None, None)
+ except TransportLaunchException, e:
+ print 'error 3'
+ config.writeMethodError(supportedTransport, e.message)
+
+ config.writeMethodEnd()
+
+ run()
diff --git a/src/examples/server.py b/src/examples/server.py
index d70c354..4340f89 100755
--- a/src/examples/server.py
+++ b/src/examples/server.py
@@ -1,4 +1,5 @@
-#!/usr/bin/env python -u
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
import os
import sys
@@ -9,36 +10,40 @@ from socket import inet_ntoa
from pyptlib.config import EnvException
from pyptlib.server import ServerConfig, MethodOptions
-supportedTransport='dummy'
+supportedTransport = 'dummy'
+
def launchServer(self, name, port):
- if name!=supportedTransport:
- raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
-
-if __name__=='__main__':
- 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()
+ if name != supportedTransport:
+ raise TransportLaunchException('Tried to launch unsupported transport %s'
+ % name)
+
+
+if __name__ == '__main__':
+ 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, e:
+ print 'error 3'
+ config.writeMethodError(supportedTransport, e.message)
+
+ config.writeMethodEnd()
diff --git a/src/pyptlib/__init__.py b/src/pyptlib/__init__.py
index e69de29..e0e3e1a 100644
--- a/src/pyptlib/__init__.py
+++ b/src/pyptlib/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
diff --git a/src/pyptlib/client.py b/src/pyptlib/client.py
index 2b58a6e..6e97422 100644
--- a/src/pyptlib/client.py
+++ b/src/pyptlib/client.py
@@ -1,43 +1,60 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
import os
from pyptlib.config.config import Config
-"""
-Configuration for a Pluggable Transport client.
-"""
-
__docformat__ = 'restructuredtext'
+
class ClientConfig(Config):
- #Public methods
- def __init__(self): # throws EnvError
- Config.__init__(self)
+ # Public methods
- self.transports=self.get('TOR_PT_CLIENT_TRANSPORTS').split(',')
- if '*' in self.transports:
- self.allTransportsEnabled=True
- self.transports.remove('*')
+ 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('*')
# 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):
- return self.transports
+
+ def getClientTransports(self):
+ return self.transports
# Write a message to stdout specifying a supported transport
# Takes: str, int, (str, int), [str], [str]
- def writeMethod(self, name, socksVersion, address, args, optArgs): # CMETHOD
- methodLine='CMETHOD %s socks%s %s:%s' % (name, socksVersion, address[0], address[1])
- if args and len(args)>0:
- methodLine=methodLine+' ARGS='+args.join(',')
- if optArgs and len(optArgs)>0:
- methodLine=methodLine+' OPT-ARGS='+args.join(',')
- print(methodLine)
+
+ def writeMethod( # CMETHOD
+ self,
+ name,
+ socksVersion,
+ address,
+ args,
+ optArgs,
+ ):
+
+ methodLine = 'CMETHOD %s socks%s %s:%s' % (name, socksVersion,
+ address[0], address[1])
+ if args and len(args) > 0:
+ methodLine = methodLine + ' ARGS=' + args.join(',')
+ if optArgs and len(optArgs) > 0:
+ methodLine = methodLine + ' OPT-ARGS=' + args.join(',')
+ 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))
+
+ 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')
+
+ def writeMethodEnd(self): # CMETHODS DONE
+ print 'CMETHODS DONE'
+
+
diff --git a/src/pyptlib/config.py b/src/pyptlib/config.py
index 148cc7a..71d63c8 100644
--- a/src/pyptlib/config.py
+++ b/src/pyptlib/config.py
@@ -1,70 +1,84 @@
-import os
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
-"""
-The base class for the client and server config classes. This class should not be used directly. Instead, use pyptlib.client or pyptlib.server.
-"""
+import os
__docformat__ = 'restructuredtext'
+
class Config:
- stateLocation=None # TOR_PT_STATE_LOCATION
- managedTransportVer=[] # TOR_PT_MANAGED_TRANSPORT_VER
- transports=[] # TOR_PT_SERVER_TRANSPORTS or TOR_PT_CLIENT_TRANSPORTS
- allTransportsEnabled=False
-
- #Public methods
-
- def __init__(self): # throws EnvError
- self.stateLocation=self.get('TOR_PT_STATE_LOCATION')
- self.managedTransportVer=self.get('TOR_PT_MANAGED_TRANSPORT_VER').split(',')
-
+
+ stateLocation = None # TOR_PT_STATE_LOCATION
+ managedTransportVer = [] # TOR_PT_MANAGED_TRANSPORT_VER
+ transports = [] # TOR_PT_SERVER_TRANSPORTS or TOR_PT_CLIENT_TRANSPORTS
+ allTransportsEnabled = False
+
+ # Public methods
+
+ def __init__(self): # throws EnvError
+ self.stateLocation = self.get('TOR_PT_STATE_LOCATION')
+ self.managedTransportVer = \
+ self.get('TOR_PT_MANAGED_TRANSPORT_VER').split(',')
+
# Returns a string representing the path to the state storage directory (which may not exist, but should be creatable) reported by Tor
- def getStateLocation(self):
- return self.stateLocation
- # Returns a list of strings representing supported versions as reported by Tor
- def getManagedTransportVersions(self):
- return self.managedTransportVer
-
+ def getStateLocation(self):
+ return self.stateLocation
+
+ # Returns a list of strings representing supported versions as reported by Tor
+
+ def getManagedTransportVersions(self):
+ return self.managedTransportVer
+
# Checks to see if the specified version is included in those reported by Tor
# Returns True if the version is included and False if it is not
- def checkManagedTransportVersion(self, version):
- return version in self.managedTransportVer
+
+ def checkManagedTransportVersion(self, version):
+ return version in self.managedTransportVer
# Returns a bool, True if the transport '*' was specified by Tor, otherwise False.
- def getAllTransportsEnabled(self):
- return self.allTransportsEnabled
-
- def checkTransportEnabled(self, transport):
- return self.allTransportsEnabled or transport in self.transports
+
+ def getAllTransportsEnabled(self):
+ return self.allTransportsEnabled
+
+ def checkTransportEnabled(self, transport):
+ return self.allTransportsEnabled or transport in self.transports
# Write a message to stdout specifying that an error parsing the environment variables has occurred
# Takes: str
- def writeEnvError(self, message): # ENV-ERROR
- print('ENV-ERROR %s' % (message))
- # Write a message to stdout specifying that the specified configuration protocol version is supported
+ def writeEnvError(self, message): # ENV-ERROR
+ print 'ENV-ERROR %s' % message
+
+ # Write a message to stdout specifying that the specified configuration protocol version is supported
# Takes: str
- def writeVersion(self, version): # VERSION
- print('VERSION %s' % (version))
+
+ def writeVersion(self, version): # VERSION
+ print 'VERSION %s' % version
# Write a message to stdout specifying that none of the specified configuration protocol versions are supported
- def writeVersionError(self): # VERSION-ERROR
- print('VERSION-ERROR no-version')
-
+
+ def writeVersionError(self): # VERSION-ERROR
+ print 'VERSION-ERROR no-version'
+
# Private methods
-
- def get(self, key):
- if key in os.environ:
- return os.environ[key]
- else:
- message="Missing environment variable %s" % (key)
- self.writeEnvError(message)
- raise EnvException(message)
-
-# Exception thrown when there is an error parsing the configuration parameters provided by Tor in environment variables
+
+ def get(self, key):
+ if key in os.environ:
+ return os.environ[key]
+ else:
+ message = 'Missing environment variable %s' % key
+ self.writeEnvError(message)
+ raise EnvException(message)
+
+
+# Exception thrown when there is an error parsing the configuration parameters provided by Tor in environment variables
+
class EnvException(Exception):
- message=None
- def __init__(self, message):
- self.message=message
+ message = None
+
+ def __init__(self, message):
+ self.message = message
+
+
diff --git a/src/pyptlib/server.py b/src/pyptlib/server.py
index 809de3b..9ece16d 100644
--- a/src/pyptlib/server.py
+++ b/src/pyptlib/server.py
@@ -1,115 +1,140 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
import os
from pyptlib.config.config import Config
-"""
-Configuration for a Pluggable Transport server.
-"""
-
__docformat__ = 'restructuredtext'
+
class ServerConfig(Config):
- extendedServerPort=None # TOR_PT_EXTENDED_SERVER_PORT
- ORPort=None # TOR_PT_ORPORT
- serverBindAddr={} # TOR_PT_SERVER_BINADDR
-
- #Public methods
-
- def __init__(self): # throws EnvError
- Config.__init__(self)
-
- self.extendedServerPort=self.get('TOR_PT_EXTENDED_SERVER_PORT')
- self.ORPort=self.get('TOR_PT_ORPORT')
-
- binds=self.get('TOR_PT_SERVER_BINDADDR').split(',')
- for bind in binds:
- key,value=bind.split('-')
- self.serverBindAddr[key]=value
-
- self.transports=self.get('TOR_PT_SERVER_TRANSPORTS').split(',')
- if '*' in self.transports:
- self.allTransportsEnabled=True
- self.transports.remove('*')
-
+
+ extendedServerPort = None # TOR_PT_EXTENDED_SERVER_PORT
+ ORPort = None # TOR_PT_ORPORT
+ serverBindAddr = {} # TOR_PT_SERVER_BINADDR
+
+ # Public methods
+
+ def __init__(self): # throws EnvError
+ Config.__init__(self)
+
+ self.extendedServerPort = self.get('TOR_PT_EXTENDED_SERVER_PORT'
+ )
+ self.ORPort = self.get('TOR_PT_ORPORT')
+
+ binds = self.get('TOR_PT_SERVER_BINDADDR').split(',')
+ for bind in binds:
+ (key, value) = bind.split('-')
+ self.serverBindAddr[key] = value
+
+ self.transports = self.get('TOR_PT_SERVER_TRANSPORTS').split(','
+ )
+ if '*' in self.transports:
+ self.allTransportsEnabled = True
+ self.transports.remove('*')
+
# Returns a tuple (str,int) representing the address of the Tor server port as reported by Tor
- def getExtendedServerPort(self):
- return self.extendedServerPort
-
+
+ def getExtendedServerPort(self):
+ return self.extendedServerPort
+
# Returns a tuple (str,int) representing the address of the Tor OR port as reported by Tor
- def getORPort(self):
- return self.ORPort
-
+
+ def getORPort(self):
+ return self.ORPort
+
# Returns a dict {str: (str,int)} representing the addresses for each transport as reported by Tor
- def getServerBindAddresses(self):
- return self.serverBindAddr
-
+
+ def getServerBindAddresses(self):
+ return self.serverBindAddr
+
# Returns a list of strings representing the server transports reported by Tor. If present, '*' is stripped from this list and used to set allTransportsEnabled to True.
- def getServerTransports(self):
- return self.transports
-
+
+ def getServerTransports(self):
+ return self.transports
+
# Write a message to stdout specifying a supported transport
# Takes: str, (str, int), MethodOptions
- def writeMethod(self, name, address, options): # SMETHOD
- if options:
- print('SMETHOD %s %s:%s %s' % (name, address[0], address[1], options))
- else:
- print('SMETHOD %s %s:%s' % (name, address[0], address[1]))
-
+
+ def writeMethod( # SMETHOD
+ self,
+ name,
+ address,
+ options,
+ ):
+
+ if options:
+ print 'SMETHOD %s %s:%s %s' % (name, address[0],
+ address[1], options)
+ else:
+ print 'SMETHOD %s %s:%s' % (name, address[0], address[1])
+
# Write a message to stdout specifying that an error occurred setting up the specified method
# Takes: str, str
- def writeMethodError(self, name, message): # SMETHOD-ERROR
- print('SMETHOD-ERROR %s %s' % (name, message))
-
+
+ def writeMethodError(self, name, message): # SMETHOD-ERROR
+ print 'SMETHOD-ERROR %s %s' % (name, message)
+
# Write a message to stdout specifying that the list of supported transports has ended
- def writeMethodEnd(self): # SMETHODS DONE
- print('SMETHODS DONE')
+
+ def writeMethodEnd(self): # SMETHODS DONE
+ print 'SMETHODS DONE'
+
class MethodOptions:
- forward=False # FORWARD
- args={} # ARGS
- declare={} # DECLARE
- useExtendedPort=False # USE-EXTENDED-PORT
-
- #Public methods
-
- def __init__(self):
- pass
-
- # Sets forward to True
- def setForward(self):
- self.forward=True
-
+
+ forward = False # FORWARD
+ args = {} # ARGS
+ declare = {} # DECLARE
+ useExtendedPort = False # USE-EXTENDED-PORT
+
+ # Public methods
+
+ def __init__(self):
+ pass
+
+ # Sets forward to True
+
+ def setForward(self):
+ self.forward = True
+
# Adds a key-value pair to args
- def addArg(self, key, value):
- self.args[key]=value
- # Adds a key-value pair to declare
- def addDeclare(self, key, value):
- self.declare[key]=value
-
+ def addArg(self, key, value):
+ self.args[key] = value
+
+ # Adds a key-value pair to declare
+
+ def addDeclare(self, key, value):
+ self.declare[key] = value
+
# Sets useExtendedPort to True
- def setUserExtendedPort(self):
- self.useExtendedPort=True
-
- def __str__(self):
- options=[]
- if self.forward:
- options.append('FORWARD:1')
- if len(self.args)>0:
- argstr='ARGS:'
- for key in self.args:
- value=self.args[key]
- argstr=argstr+key+'='+value+','
- argstr=argstr[:-1] # Remove trailing comma
- options.append(argstr)
- if len(self.declare)>0:
- decs='DECLARE:'
- for key in self.declare:
- value=self.declare[key]
- decs=decs+key+'='+value+','
- decs=decs[:-1] # Remove trailing comma
- options.append(decs)
- if self.useExtendedPort:
- options.append('USE-EXTENDED-PORT:1')
-
- return ' '.join(options)
+
+ def setUserExtendedPort(self):
+ self.useExtendedPort = True
+
+ def __str__(self):
+ options = []
+ if self.forward:
+ options.append('FORWARD:1')
+ if len(self.args) > 0:
+ argstr = 'ARGS:'
+ for key in self.args:
+ value = self.args[key]
+ argstr = argstr + key + '=' + value + ','
+ argstr = argstr[:-1] # Remove trailing comma
+ options.append(argstr)
+ if len(self.declare) > 0:
+ decs = 'DECLARE:'
+ for key in self.declare:
+ value = self.declare[key]
+ decs = decs + key + '=' + value + ','
+ decs = decs[:-1] # Remove trailing comma
+ options.append(decs)
+ if self.useExtendedPort:
+ options.append('USE-EXTENDED-PORT:1')
+
+ return ' '.join(options)
+
+
diff --git a/src/pyptlib/util.py b/src/pyptlib/util.py
index e307e5c..ced88b1 100644
--- a/src/pyptlib/util.py
+++ b/src/pyptlib/util.py
@@ -1,3 +1,6 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
import re
import sys
import binascii
@@ -5,131 +8,155 @@ import binascii
from struct import pack, unpack
from socket import inet_aton, inet_ntoa
-v3=(sys.version[0]=='3')
+v3 = sys.version[0] == '3'
+
def uncompact(x):
- ip, port = unpack("!4sH", x)
- return inet_ntoa(ip), port
+ (ip, port) = unpack('!4sH', x)
+ return (inet_ntoa(ip), port)
+
def encode(s):
- return binascii.hexlify(s).decode('ascii')
+ return binascii.hexlify(s).decode('ascii')
+
def decode(s):
- return binascii.unhexlify(s.encode('ascii'))
+ return binascii.unhexlify(s.encode('ascii'))
+
def encodeAddress(addr):
- ip=addr[0]
- if ip=='':
- ip='::'
- port=addr[1]
- if '.' in ip:
- return ip+':'+str(port)
- else:
- return '['+ip+']:'+str(port)
+ ip = addr[0]
+ if ip == '':
+ ip = '::'
+ port = addr[1]
+ if '.' in ip:
+ return ip + ':' + str(port)
+ else:
+ return '[' + ip + ']:' + str(port)
+
def decodeAddress(s):
- if '.' in s:
- parts=s.split(':')
- return (parts[0], int(parts[1]), False)
- else:
- m=re.match('\[([0-9a-f:]+)\]:([0-9]+)', s)
- return (m.group(1), int(m.group(2)), True)
+ if '.' in s:
+ parts = s.split(':')
+ return (parts[0], int(parts[1]), False)
+ else:
+ m = re.match('\[([0-9a-f:]+)\]:([0-9]+)', s)
+ return (m.group(1), int(m.group(2)), True)
+
def getAddress(port):
- return encodeAddress((getPublicIP(), port))
+ return encodeAddress((getPublicIP(), port))
+
def splitFields(msg, fields, optionalData=False):
- values=[]
- for field in fields:
- value=msg[:field]
- msg=msg[field:]
- values.append(value)
- if len(msg)>0:
- values.append(msg)
- elif optionalData:
- values.append(None)
- return values
+ values = []
+ for field in fields:
+ value = msg[:field]
+ msg = msg[field:]
+ values.append(value)
+ if len(msg) > 0:
+ values.append(msg)
+ elif optionalData:
+ values.append(None)
+ return values
+
def splitField(msg, field):
- return msg[:field], msg[field:]
+ return (msg[:field], msg[field:])
+
def decodeFlags(flagsByte):
- from bitstring import BitString
- bits=BitString(bytes=flagsByte)
- bools=[]
- for x in range(bits.length):
- bools.append(bits.readbit().uint==1)
- return bools
+ from bitstring import BitString
+ bits = BitString(bytes=flagsByte)
+ bools = []
+ for x in range(bits.length):
+ bools.append(bits.readbit().uint == 1)
+ return bools
+
def encodeFlags(bools):
- from bitstring import BitString
- bits=BitString()
- for bool in bools:
- if bool:
- bits.append(BitString('0b1'))
- else:
- bits.append(BitString('0b0'))
- return bits.bytes
+ from bitstring import BitString
+ bits = BitString()
+ for bool in bools:
+ if bool:
+ bits.append(BitString('0b1'))
+ else:
+ bits.append(BitString('0b0'))
+ return bits.bytes
+
def fill(bs, size):
- while len(bs)<size:
+ while len(bs) < size:
+ if v3:
+ filler = bytes('\x00', 'ascii')
+ else:
+ filler = '\x00'
+ bs = bs + filler
+ return bs
+
+
+def xor(a, b):
+ if len(a) != len(b):
+ print ('xor parameters must be the same length:', len(a),
+ len(b))
+ return None
if v3:
- filler=bytes('\x00', 'ascii')
+ c = bytearray()
+ for x in range(len(a)):
+ c.append(a[x] ^ b[x])
+ return bytes(c)
else:
- filler='\x00'
- bs=bs+filler
- return bs
+ c = ''
+ for x in range(len(a)):
+ c = c + chr(ord(a[x]) ^ ord(b[x]))
+ return c
-def xor(a, b):
- if len(a)!=len(b):
- print('xor parameters must be the same length:', len(a), len(b))
- return None
- if v3:
- c=bytearray()
- for x in range(len(a)):
- c.append(a[x] ^ b[x])
- return bytes(c)
- else:
- c=''
- for x in range(len(a)):
- c=c+chr(ord(a[x]) ^ ord(b[x]))
- return c
if v3:
- from urllib.request import urlopen
+ from urllib.request import urlopen
else:
- from urllib2 import urlopen
+ from urllib2 import urlopen
+
def getPublicIP(v6=True):
- if v6:
+ if v6:
+
# try:
- text=urlopen("http://ipv6.ip6.me/").read()
- if v3:
- match=re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
- else:
- match=re.search("\+3>([^<]+)<", text)
- ip=match.group(1)
- ip=ip.decode('ascii')
- return ip
+
+ text = urlopen('http://ipv6.ip6.me/').read()
+ if v3:
+ match = re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
+ else:
+ match = re.search("\+3>([^<]+)<", text)
+ ip = match.group(1)
+ ip = ip.decode('ascii')
+ return ip
+ else:
+
# except Exception as e:
# print(e)
# ip=urlopen("http://whatismyv6ip.com/myip").read()
# return ip.decode('ascii')
- else:
- text=urlopen("http://ip4.me/").read()
- if v3:
- match=re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
- else:
- match=re.search("\+3>([^<]+)<", text)
+
+ text = urlopen('http://ip4.me/').read()
+ if v3:
+ match = re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
+ else:
+ match = re.search("\+3>([^<]+)<", text)
+
# ip=urlopen("http://whatismyv6ip.com/myip").read()
# return ip.decode('ascii')
- ip=match.group(1)
- ip=ip.decode('ascii')
- return ip
+
+ ip = match.group(1)
+ ip = ip.decode('ascii')
+ return ip
+
def randomPort():
- import random
- minPort=5000
- maxPort=10000
- port=random.randint(minPort, maxPort)
- return port
+ import random
+ minPort = 5000
+ maxPort = 10000
+ port = random.randint(minPort, maxPort)
+ return port
+
+
--
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