twisted

Testing with Twisted and inlineCallbacks

◇◆丶佛笑我妖孽 提交于 2019-12-06 14:12:46
Here is my function definition: @defer.inlineCallbacks def get_order(order_id): # do some db operations... defer.returnValue(order_details) What I want to do is to test this function using Twisted's trial: from twisted.trial import unittest from twisted.internet import defer class OrderTest(unittest.TestCase): @defer.inlineCallbacks def test_order(self): order = yield get_order(5) raise Exception('FAIL FAIL!!') # this should fail, but never does self.assertEqual(order.id, 6) I'm very confused.. I red all the documents that I could find about Twisted and trial but couldn't find how to make this

Using Tornado and Twisted at the same time

允我心安 提交于 2019-12-06 13:44:59
I am in a weird situation where I have to use Twisted in a system built completely out of Tornado. They can share the same IOLoop so I know they can work together. My question is can I safely use their co-routine decorators in the same function? For example: import tornado.platform.twisted tornado.platform.twisted.install() ... @gen.engine @defer.inlineCallbacks def get(self): ... a = yield gen.Task(getA) # tornado b = yield proxy.callRemote(getB) # twisted ... defer.returnValue(a + b) # twisted They do work on the same IOLoop so I am thinking this should be fine. Would there be any unforeseen

Cleanup of resources with the SMTP module of Twisted Python

不羁岁月 提交于 2019-12-06 11:12:23
This is related to the previously answered question here: Logging SMTP connections with Twisted . I have a database resource that I create in each instance of ConsoleMessageDelivery that I need to ensure is cleaned up when the socket is closed. I have a WrappingFactory called DenyFactory and the DenyFactory.unregisterProtocol method is called when the socket is closed, but I have no way (that I can figure out) how to access the resource created in the ConsoleMessageDelivery instance that's being destroyed. I tried a del () method in ConsoleMessageDelivery but that's never called. What's the

Twisted reactor starting multiple times in a single program?

五迷三道 提交于 2019-12-06 09:44:19
问题 Is it possible to start the reactor more than once in the same program? Suppose you wanted to encapsulate twisted functionality inside a method, for API purposes. For example, mymodule.py looks like this: 1 from twisted.web.client import getPage 2 from twisted.internet import reactor 3 4 def _result(r): 5 print r 6 reactor.stop() 7 8 def _error(e): 9 print e 10 reactor.stop() 11 12 def getGoogle(): 13 d = getPage('http://www.google.com') 14 d.addCallbacks(_result, _error) 15 reactor.run() 16

'twistd' is not a recognized internal or external command

霸气de小男生 提交于 2019-12-06 09:12:15
I'm trying to develop a Twisted Web server but can't seem to run the twistd command. I've tried setting the python path and even included the path to the twistd.py script in my Path but nothing seems to work. I'm using Twisted 12.0.0 and Python 2.7 on Windows. Any help would be hugely appreciated. You need to set the %PATHEXT% environment variable to include .py , as well as %PATH% including the path to twistd . Your most-recently-installed version of Python should then automatically launch it, assuming the filetype association was set correctly by the installer. Create a twistd.bat and save

Python ssh client over socks (proxy)

坚强是说给别人听的谎言 提交于 2019-12-06 08:11:37
问题 So, I need to connect to SSH server through proxy socks. I read paramiko and twisted.conch docs, but didn`t found proxy socks support there. 回答1: This socket-wrapper allows you to use static ssh-tunnels. I found a common solution for my problem: Use paramiko SSHClient class Extend SSHClient with your own class Re-implement the connect() method: Instead of using a standard socket object we pass to it a fixed proxied socket from the python package sockipy 回答2: Paraproxy (a Paramiko addon for

Writing excellent Twisted web Resources

十年热恋 提交于 2019-12-06 07:21:43
I wrote my very first Twisted 10.1.0 web Resource and I am seeking for feedback, because I feel this isn't exactly following the best practice and may contain newbies bugs. The resource responds to /?url=http://www.foo.baz/abc123 and relies on a service that returns a dict . If anything goes wrong (e.g., invalid or non-existing url , then a 400 is returned). Any comment? Anything to fix, to improve class ProcessedUrl(resource.Resource): isLeaf = True def __init__(self, service): resource.Resource.__init__(self) self.service = service def _cancel(self, err, deferred): deferred.cancel() def

How to test LoopingCall()?

梦想的初衷 提交于 2019-12-06 07:01:50
问题 Within my code, I use task.LoopingCall() to run some deferred function every second. I want to make sure that that function returns the right values for a certain number of things. So, I thought I could use a task.clock() and call the advance() method on it. However, I am not getting the right number of responses expected. Any idea what I am doing wrong? Here is a test code to show what I mean. First is the server: from twisted.internet.protocol import Factory from twisted.protocols.basic

How to unit test code that makes HTTP requests with twisted.web.client.Agent?

独自空忆成欢 提交于 2019-12-06 06:36:40
问题 I'm looking for hints or examples that illustrate how to unit test code that makes HTTP requests using twisted.web.client.Agent. Is it possible to use the real Agent in tests and configure it to connect to a fake HTTP server using StringTransport (no real TCP connetion)? Or is it better to mock out the Agent and inject a mock into classes that use the Agent? 回答1: it is certainly possible to use a real Agent . You must construct the Agent instance with a reactor as first argument; as such, you

Python + Twisted + sqlanydb = abort()

半世苍凉 提交于 2019-12-06 06:13:02
I'm using Twisted 11 together with SQLAnywhere 12 via the official sqlanydb driver. Generally, it works fine. But occasionally the application crashes with an abort on the first query. If one query worked, all following work too. However my tests run only seldom through. That's awful to develop with and strace doesn't tell me anything informative too. Sometimes it crashes inside of select(), sometimes in mmap()... I'm running 64bit Linux and run locally the Sybase as dbeng12 for testing. Is anyone working successfully using these components? Any suggestions how to solve that? I used sqlanydb