twisted

How to make Twisted use Python logging?

牧云@^-^@ 提交于 2019-12-03 18:35:50
问题 I've got a project where I'm using Twisted for my web server. When exceptions occur (such as network errors), it's printing to the console. I've already got logging through Python's built-in log module - is there any way to tell the reactor to use that instead? What's the usual pattern for this? 回答1: Found it. It's actually quite easy: from twisted.python import log observer = log.PythonLoggingObserver(loggerName='logname') observer.start() You just set loggerName to the same logger name that

How can I write tests for code using twisted.web.client.Agent and its subclasses?

二次信任 提交于 2019-12-03 17:36:39
I read the official tutorial on test-driven development, but it hasn't been very helpful in my case. I've written a small library that makes extensive use of twisted.web.client.Agent and its subclasses ( BrowserLikeRedirectAgent , for instance), but I've been struggling in adapting the tutorial's code to my own test cases. I had a look at twisted.web.test.test_web , but I don't understand how to make all the pieces fit together. For instance, I still have no idea how to get a Protocol object from an Agent , as per the official tutorial Can anybody show me how to write a simple test for some

twisted: catch keyboardinterrupt and shutdown properly

前提是你 提交于 2019-12-03 16:52:19
问题 UPDATE: For ease of reading, here is how to add a callback before the reactor gets shutdown: reactor.addSystemEventTrigger('before', 'shutdown', callable) Original question follows. If I have a client connected to a server, and it's chilling in the reactor main loop waiting for events, when I hit CTRL-C, I get a "Connection to the other side was lost in a non-clean fashion: Connection lost." How can I set it up so that I know when a KeyboardInterrupt happens, so that I can do proper clean-up

Twisted Python script on Raspberry Pi (Debian) to communicate with Arduino via USB

 ̄綄美尐妖づ 提交于 2019-12-03 16:40:27
I have been working on an Arduino/Raspberry Pi project where I have found myself learning not just Python but Twisted Python as well; so I apologize in advance for my newbness. I am trying to keep it simple for now and just trying to send a char at any one time between the two devices. So far I am able to send from the Raspberry Pi to the Arduino and effectively turn its LED off/on just as expected. However I cannot seem to generate Twisted code which will detect anything coming from the Arduino to the RPi on the serial port. I verified that the Arduino is sending chars every 2 seconds with a

Redirecting before POST upload has been completed

梦想与她 提交于 2019-12-03 15:06:28
问题 I have form with file upload. The files to be uploaded actually are pictures and videos, so they can be quite big. I have logic which based on headers and first 1KB can determine if the rest will be processed or immediately rejected. In the later case I'd like to redirect client to error page without having to wait for upload to finish. The case is, that just sending response before POST is complete doesn't seem to work. The redirect gets ignored and if I close connection, browser complains

What is the practical difference between xml, json, rss and atom when interfacing with Twitter?

有些话、适合烂在心里 提交于 2019-12-03 14:49:46
I'm new to web services and as an introduction I'm playing around with the Twitter API using the Twisted framework in python. I've read up on the different formats they offer, but it's still not clear to me which one I should use in my fairly simple project. Specifically the practical difference between using JSON or XML is something I'd like guidance on. All I'm doing is requesting the public timeline and caching it locally. Thanks. For me it boils down to convenience. Using XML, I have to parse the response in to a DOM (or more usually an ElementTree). Using JSON, one call to simplejson

How to declare ports in Cloud9 using Python

谁说我不能喝 提交于 2019-12-03 14:38:43
I'm new to using Cloud9 IDE (c9) and so far it looks great, except a few minor things. I see from the docs that to start up a simple node.js http server, you have to pass in process.env.PORT in place of the regular port such as "8080". Node Hello World example : var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(process.env.PORT, process.env.IP); What I want to know is, on c9, can you only start services on ports using javascript / node.js? Or do other languages work just as well, perhaps

twisted deferred/callbacks and asynchronous execution

不想你离开。 提交于 2019-12-03 14:15:56
I'm trying to figure out how can i make my code more asynchronous using twisted. A function returns a deferred object then i add a list of callbacks the first callback will be called after the deferred function provides some result through deferred_obj.callback then, in the chain of callbacks, the first callback will do something with the data and call the second callback and etc. however chained callbacks will not be considered asynchronous because they're chained and the event loop will keep firing each one of them concurrently until there is no more, right? However, if I have a deferred

Python - How can I make this code asynchronous?

删除回忆录丶 提交于 2019-12-03 12:21:14
Here's some code that illustrates my problem: def blocking1(): while True: yield 'first blocking function example' def blocking2(): while True: yield 'second blocking function example' for i in blocking1(): print 'this will be shown' for i in blocking2(): print 'this will not be shown' I have two functions which contain while True loops. These will yield data which I will then log somewhere (most likely, to an sqlite database). I've been playing around with threading and have gotten it working. However, I don't really like it... What I would like to do is make my blocking functions

twisted http client

烈酒焚心 提交于 2019-12-03 11:43:22
问题 I am after an example describing the usage of Twisted's HTTP Client. After reading the excellent blog post on the internals of Twisted, I understand how the "Factory" and "Protocol" components play their role but I am unclear on how to introduce "Request" in the overall Client flow. More specifically, I need to be able to perform HTTP GET and POST requests to a remote server using Twisted. Updated : after a discussion on irc #twisted / #python, it seems that twisted.web2 is fading away in