Here are the examples of the python api twisted.python taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
18 Examples
5
Example 1
Project: mythbox Source File: test_reflect.py
def test_namedAnyPackageLookup(self):
"""
L{namedAny} should return the package object for the name it is passed.
"""
import twisted.python
self.assertIdentical(
reflect.namedAny("twisted.python"), twisted.python)
5
Example 2
def test_package(self):
"""
L{reflect.fullyQualifiedName} returns the full name of a package and
a subpackage.
"""
import twisted
self._checkFullyQualifiedName(twisted, 'twisted')
import twisted.python
self._checkFullyQualifiedName(twisted.python, 'twisted.python')
3
Example 3
Project: splash Source File: server.py
def start_logging(opts):
import twisted
from twisted.python import log
if opts.logfile:
from twisted.python.logfile import DailyLogFile
logfile = DailyLogFile.fromFullPath(opts.logfile)
else:
logfile = sys.stderr
flo = log.startLogging(logfile)
if twisted.version.major >= 13: # add microseconds to log
flo.timeFormat = "%Y-%m-%d %H:%M:%S.%f%z"
3
Example 4
Project: splash Source File: server.py
def bump_nofile_limit():
from twisted.python import log
log.msg("Open files limit: %d" % resource.getrlimit(resource.RLIMIT_NOFILE)[0])
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
values_to_try = [v for v in [hard, 100000, 10000] if v > soft]
for new_soft in values_to_try:
try:
resource.setrlimit(resource.RLIMIT_NOFILE, (new_soft, hard))
except ValueError:
continue
else:
log.msg("Open files limit increased from %d to %d" % (soft, new_soft))
break
else:
log.msg("Can't bump open files limit")
3
Example 5
Project: splash Source File: server.py
def _default_proxy_factory(proxy_profiles_path):
from twisted.python import log
from splash import proxy
if proxy_profiles_path is not None:
if os.path.isdir(proxy_profiles_path):
log.msg("proxy profiles support is enabled, "
"proxy profiles path: %s" % proxy_profiles_path)
else:
log.msg("--proxy-profiles-path does not exist or it is not a folder; "
"proxy won't be used")
proxy_profiles_path = None
return functools.partial(proxy.get_factory, proxy_profiles_path)
3
Example 6
Project: splash Source File: server.py
def _check_js_profiles_path(js_profiles_path):
from twisted.python import log
if js_profiles_path is not None and not os.path.isdir(js_profiles_path):
log.msg("--js-profiles-path does not exist or it is not a folder; "
"js profiles won't be used")
return js_profiles_path
3
Example 7
Project: Piped Source File: scripts.py
def _find_plugin_versions(self):
from twisted.python import modules
versions = dict()
plugin_module = modules.getModule('piped.plugins')
plugin_module.load()
for module in plugin_module.iterModules():
versions[module.name] = getattr(module.load(), 'version', 'unknown version')
return versions
3
Example 8
Project: Piped Source File: scripts.py
def _run_twistd_with_config(twistd_config):
if twistd_config['D']:
# replace the default failure object with one that performs no cleaning
from twisted.python import failure
from piped import util
failure._OriginalFailure = failure.Failure
failure.Failure = util.NonCleaningFailure
# and enable debugging on deferreds, so we can properly inspect post-mortem
from twisted.internet import defer
defer.setDebugging(True)
sys.argv[0] = 'twistd' # pretend to be twistd, so that piped.log doesn't decide to bootstrap the logging system
twistd.runApp(twistd_config)
3
Example 9
Project: obfsproxy Source File: network.py
def ensure_outgoing_proxy_dependencies():
"""Make sure that we have the necessary dependencies to connect to
outgoing HTTP/SOCKS proxies.
Raises OutgoingProxyDepsFailure in case of error.
"""
# We can't connect to outgoing proxies without txsocksx.
try:
import txsocksx
except ImportError:
raise OutgoingProxyDepsFailure("We don't have txsocksx. Can't do proxy. Please install txsocksx.")
# We also need a recent version of twisted ( >= twisted-13.2.0)
import twisted
from twisted.python import versions
if twisted.version < versions.Version('twisted', 13, 2, 0):
raise OutgoingProxyDepsFailure("Outdated version of twisted (%s). Please upgrade to >= twisted-13.2.0" % twisted.version.short())
3
Example 10
def spewer(frame, s, ignored):
"""A trace function for sys.settrace that prints every function or method call."""
from twisted.python import reflect
if frame.f_locals.has_key('self'):
se = frame.f_locals['self']
if hasattr(se, '__class__'):
k = reflect.qual(se.__class__)
else:
k = reflect.qual(type(se))
print 'method %s of %s at %s' % (
frame.f_code.co_name, k, id(se)
)
else:
print 'function %s in %s, line %s' % (
frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno)
3
Example 11
Project: TwistedBot Source File: util.py
def spewer(frame, s, ignored):
"""
A trace function for sys.settrace that prints every function or method call.
"""
from twisted.python import reflect
if frame.f_locals.has_key('self'):
se = frame.f_locals['self']
if hasattr(se, '__class__'):
k = reflect.qual(se.__class__)
else:
k = reflect.qual(type(se))
print 'method %s of %s at %s' % (
frame.f_code.co_name, k, id(se)
)
else:
print 'function %s in %s, line %s' % (
frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno)
0
Example 12
Project: splash Source File: server.py
def log_splash_version():
import twisted
from twisted.python import log
from splash import lua
from splash.qtutils import get_versions
log.msg("Splash version: %s" % __version__)
verdict = get_versions()
versions = [
"Qt %s" % verdict['qt'],
"PyQt %s" % verdict['pyqt'],
"WebKit %s" % verdict['webkit'],
"sip %s" % verdict['sip'],
"Twisted %s" % twisted.version.short(),
]
if lua.is_supported():
versions.append(lua.get_version())
log.msg(", ".join(versions))
log.msg("Python %s" % sys.version.replace("\n", ""))
0
Example 13
Project: opencanary Source File: ssh.py
def getRSAKeys():
public_key = os.path.join(SSH_PATH, 'id_rsa.pub')
private_key = os.path.join(SSH_PATH, 'id_rsa')
if not (os.path.exists(public_key) and os.path.exists(private_key)):
from Crypto.PublicKey import RSA
from twisted.python import randbytes
KEY_LENGTH = 2048
rsaKey = RSA.generate(KEY_LENGTH, randbytes.secureRandom)
publicKeyString = keys.Key(rsaKey).public().toString('openssh')
privateKeyString = keys.Key(rsaKey).toString('openssh')
with file(public_key, 'w+b') as f:
f.write(publicKeyString)
with file(private_key, 'w+b') as f:
f.write(privateKeyString)
else:
with file(public_key) as f:
publicKeyString = f.read()
with file(private_key) as f:
privateKeyString = f.read()
return publicKeyString, privateKeyString
0
Example 14
Project: opencanary Source File: ssh.py
def getDSAKeys():
public_key = os.path.join(SSH_PATH, 'id_dsa.pub')
private_key = os.path.join(SSH_PATH, 'id_dsa')
if not (os.path.exists(public_key) and os.path.exists(private_key)):
from Crypto.PublicKey import DSA
from twisted.python import randbytes
KEY_LENGTH = 1024
dsaKey = DSA.generate(KEY_LENGTH, randbytes.secureRandom)
publicKeyString = keys.Key(dsaKey).public().toString('openssh')
privateKeyString = keys.Key(dsaKey).toString('openssh')
with file(public_key, 'w+b') as f:
f.write(publicKeyString)
with file(private_key, 'w+b') as f:
f.write(privateKeyString)
else:
with file(public_key) as f:
publicKeyString = f.read()
with file(private_key) as f:
privateKeyString = f.read()
return publicKeyString, privateKeyString
0
Example 15
Project: flumotion Source File: test_component_httpserver_httpcached_stats.py
def setUp(self):
from twisted.python import threadpool
reactor.threadpool = threadpool.ThreadPool(0, 10)
reactor.threadpool.start()
class Hello(resource.Resource):
isLeaf = True
def render_GET(self, request):
return "<html>Hello, world!</html>"
self.src_path = tempfile.mkdtemp(suffix=".src")
self.cache_path = tempfile.mkdtemp(suffix=".cache")
self._resource = None
self.createSrcFile("a", "content of a")
src = File(self.src_path)
src.putChild("hello", Hello())
factory = Site(src)
self.httpserver = reactor.listenTCP(0, factory)
p = self.httpserver.getHost().port
plugProps = {"properties": {"cache-size": CACHE_SIZE,
"cache-dir": self.cache_path,
"virtual-hostname": "localhost",
"virtual-port": p}}
self.plug = \
file_provider.FileProviderHTTPCachedPlug(plugProps)
self.stats = DummyStats()
self.plug.startStatsUpdates(self.stats)
return self.plug.start(None)
0
Example 16
Project: calvin-base Source File: test_dht_server.py
@pytest.inlineCallbacks
def atest_dht_single(self, monkeypatch):
from twisted.python import log
log.startLogging(sys.stdout)
iface = "0.0.0.0"
a = None
b = None
q = Queue.Queue()
defer.setDebugging(True)
def server_started(a, *args):
for b in args:
if isinstance(b, twisted.python.failure.Failure):
b.printTraceback()
else:
_log.debug("** %s" % b)
q.put([a, args])
try:
a = AutoDHTServer()
a.start(iface, cb=CalvinCB(server_started, "1"))
# Wait for start
assert q.get(timeout=2) == ["1", self._sucess_start]
assert a.set(key="APA", value="banan").wait() == True
assert a.get(key="APA").wait() == "banan"
except Exception as e:
_log.exception("Failed")
pytest.fail(traceback.format_exc())
finally:
if a:
yield threads.defer_to_thread(a.stop)
0
Example 17
Project: calvin-base Source File: test_dht_server.py
@pytest.inlineCallbacks
def test_callback(self, monkeypatch):
from twisted.python import log
log.startLogging(sys.stdout)
iface = "0.0.0.0"
a = None
b = None
q = Queue.Queue()
def server_started(aa, *args):
for b in args:
if isinstance(b, twisted.python.failure.Failure):
b.printTraceback()
else:
_log.debug("** %s" % b)
q.put([aa,args])
def set_cb(*args):
_log.debug("** %s" % repr(args))
q.put(args)
def get_cb(*args):
_log.debug("** %s" % repr(args))
q.put(args)
try:
amount_of_servers = 3
a = AutoDHTServer()
a.start(iface, cb=CalvinCB(server_started, "1"))
b = AutoDHTServer()
b.start(iface, cb=CalvinCB(server_started, "2"))
c = AutoDHTServer()
c.start(iface, cb=CalvinCB(server_started, "3"))
servers = []
while servers.__len__() < amount_of_servers:
try:
server = yield threads.defer_to_thread(q.get, timeout=2)
except Queue.Empty:
_log.debug("Queue empty!")
break
if server not in servers:
servers.append(server)
_log.debug("Servers added: {}.".format(servers))
else:
_log.debug("Server: {} already started.".format(server))
for server in range(1, amount_of_servers + 1):
assert [str(server), self._sucess_start] in servers
yield threads.defer_to_thread(time.sleep, 1)
yield threads.defer_to_thread(q.queue.clear)
a.set(key="APA", value="banan", cb=CalvinCB(set_cb))
set_value = yield threads.defer_to_thread(q.get, timeout=2)
assert ("APA", True) == set_value
yield threads.defer_to_thread(time.sleep, 1)
yield threads.defer_to_thread(q.queue.clear)
a.get(key="APA", cb=CalvinCB(get_cb))
get_value = yield threads.defer_to_thread(q.get, timeout=2)
assert get_value == ("APA", "banan")
yield threads.defer_to_thread(time.sleep, 1)
yield threads.defer_to_thread(q.queue.clear)
b.get(key="APA", cb=CalvinCB(get_cb))
get_value = yield threads.defer_to_thread(q.get, timeout=2)
assert get_value == ("APA", "banan")
except Exception as e:
_log.exception("Failed")
pytest.fail(traceback.format_exc())
finally:
if a:
a.stop()
if b:
b.stop()
if c:
c.stop()
yield threads.defer_to_thread(time.sleep, 1)
0
Example 18
def run_trial():
from twisted.python import modules
# We monkey patch PythonModule in order to avoid tests inside namespace packages
# being run multiple times (see http://twistedmatrix.com/trac/ticket/5030)
PythonModule = modules.PythonModule
class PatchedPythonModule(PythonModule):
def walkModules(self, *a, **kw):
yielded = set()
# calling super on PythonModule instead of PatchedPythonModule, since we're monkey patching PythonModule
for module in super(PythonModule, self).walkModules(*a, **kw):
if module.name in yielded:
continue
yielded.add(module.name)
yield module
modules.PythonModule = PatchedPythonModule
from twisted.scripts.trial import run
run()