tornado

Python. Tornado. Non-blocking xmlrpc client

旧城冷巷雨未停 提交于 2019-12-09 13:51:18
问题 Basically we can call xmlrpc handlers following way: import xmlrpclib s = xmlrpclib.ServerProxy('http://remote_host/rpc/') print s.system.listmethods() In tornado we can integrate it like this: import xmlrpclib import tornado.web s = xmlrpclib.ServerProxy('http://remote_host/rpc/') class MyHandler(tornado.web.RequestHandler): def get(self): result = s.system.listmethods() I have following, a little bit newbie, questions: Will result = s.system.listmethods() block tornado? Are there any non

将任意Bytecode注入运行中的Python进程

筅森魡賤 提交于 2019-12-09 13:40:45
在调试 Python 程序的时候,一般我们只能通过以下几种方式进行调试: 1. 程序中已经有的日志 2. 在代码中插入 import pdb; pdb.set_trace() 但是以上的方法也有不方便的地方, 比如对于已经在运行中的程序, 就不可能停止程序后加入 调试代码和增加新的日志. 从 JAVA 的 BTrace (https://kenai.com/projects/btrace) 项目得到灵感,尝试对正在运行的 Python 进程插入代码,在程序运行到指定的函数后,自动连接远程主机进行调试 首先介绍三个开源的项目, 本实验需要用到这三个项目 1. Pyasite https://github.com/lmacken/pyrasite Tools for injecting code into running Python processes 2. Byteplay https://github.com/serprex/byteplay 一个字节码维护项目,类似 java的asm/cglib 3. Rpdb-Shell https://github.com/alex8224/Rpdb-Shell 待注入的代码, 用官方的 ``tornado hello demo`` 做例子 import tornado.ioloop import tornado.web import os

Better way to handle errors in tornado request handler

核能气质少年 提交于 2019-12-09 10:20:48
问题 There are two similar handlers: AgeHandler1 and AgeHandler2. In the first one we simply raise a specific exception to return an error message, in the second - we manually return an error message. What You think about these two methods? Which method is preferable for a large project? Any other best practices? import logging import os.path import traceback from sys import exc_info from tornado import web, options, ioloop logger = logging.getLogger(__name__) class MyAppException(Exception): def

tornado what is the difference between @web.asynchronous @gen.coroutine VS @gen.corutine

亡梦爱人 提交于 2019-12-09 06:42:04
问题 In the document, @web.asynchronous is unnecessary if the method is also decorated with @gen.coroutine. like this @web.asynchronous @gen.coroutine def get(self): ... but, In document, They also explain that If you use @web.asynchronous, then you should call self.finish(). However, In above case(using two decorator together) the connection is finished with out calling "self.finish()" I'm wondering what happened in there. and In below case, It works different with above. @web.asynchronous def

将一个简单的python程序打包成加密的docker镜像并对外提供接口

坚强是说给别人听的谎言 提交于 2019-12-08 23:39:58
环境:python2.7 docker: 一、一个简单的python程序 既然是一个简单的python程序,那我们就实现一个简单的加法功能即可。 #coding=utf-8 import random def add(aStr,bStr): map={} try: a=float(aStr) b=float(bStr) sum=a+b file_name=str(int(random.random()*1000000))+"" file=open("data/"+file_name+'.txt',"w") file.write(str(a)+"+"+str(b)+"="+str(sum)) file.close() map["result"]="OK" map["id"]=str(file_name) except: map["result"]="wrong" return map if __name__=="__main__": print "运行run啊大哥,不是这个" map=add("4.5",5.6) print map 这个方法实现的功能都能看明白,就是把a和b相加,并且将结果输出在一个文件中,并返回是否运行成功,如果成功则再加上文件的编号(后面那段不管= =) 二、对外提供一个调用接口 除了这段代码我们还需要对外提供一个调用接口

Tornado - Python global variable

懵懂的女人 提交于 2019-12-08 18:13:32
I'm trying to use Tornado with SqlAlchemy, I need to pass the current user from RequestHandler (tornado) to models (SqlAlchemy) in the insert or update action. But I don't want to pass the value directly to the model, example: #### RequestHandler POST method... user = Session.query(User).get(1) user.name = "bla, bla, bla..." user.updated_by = self.current_user # don't use... session.commit() I'm using a global variable, in a __ init__.py file, and set the current user value in the RequestHandler and after, get the value, in before update event with SqlAlchemy. The idea is to know what user is

Post to Tornado server

落花浮王杯 提交于 2019-12-08 15:46:42
问题 I am trying to post to my Tornado Web Server but keep getting a 405 Error. Not sure what is going wrong. I am fairly new to python but I have been searching up on this for about a month and finally decided to give it a go. Tornado Web Server: import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web from tornado.options import define, options import os import string from time import sleep from datetime import datetime import hashlib import json from tornado

Tornado rediret incur TypeError: initialize() missing 1 required positional argument: 'url'

你离开我真会死。 提交于 2019-12-08 15:18:34
问题 I am new to tornado and web service development, and currently implementing a tiny web for practising tornado module. This is the python code I used for my server import bcrypt import concurrent.futures #import MySQLdb #import markdown import os.path import re import subprocess #import torndb import tornado.escape from tornado import gen import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import unicodedata from tornado.options import define, options

Use tornado future to fetch url, two different ways get different results

╄→гoц情女王★ 提交于 2019-12-08 13:13:27
问题 I want to use tornado to fetch batch urls. So my code shows below: from tornado.concurrent import Future from tornado.httpclient import AsyncHTTPClient from tornado.ioloop import IOLoop class BatchHttpClient(object): def __init__(self, urls, timeout=20): self.async_http_client = AsyncHTTPClient() self.urls = urls self.timeout = 20 def __mid(self): results = [] for url in self.urls: future = Future() def f_callback(f1): future.set_result(f1.result()) f = self.async_http_client.fetch(url) f.add

What is a good way to organize your models, connections if one wants to use SQLAlchemy to connect several databases to various applications?

萝らか妹 提交于 2019-12-08 12:46:24
问题 Background: This is the situation I am facing and so far my current solution seems rather clunky. I want to improve on it. Right now: I setup connections to each database in the main function of the Pyramid application: def main(global_config, **settings): a_engine = engine_from_config(settings, 'A.') b_engine = engine_from_config(settings, 'B.') ASession.configure(bind=a_engine) BSession.configure(bind=b_engine) "ASession" and "BSession" are simply globally defined scoped_session in /models/