如何在Bottle中设置我的响应的HTTP状态代码?
from bottle import app, run, route, Response
@route('/')
def f():
Response.status = 300 # also tried `Response.status_code = 300`
return dict(hello='world')
'''StripPathMiddleware defined:
http://bottlepy.org/docs/dev/recipes.html#ignore-trailing-slashes
'''
run(host='localhost', app=StripPathMiddleware(app()))
Run Code Online (Sandbox Code Playgroud)
如您所见,输出不会返回我设置的HTTP状态代码:
$ curl localhost:8080 -i
HTTP/1.0 200 OK
Date: Sun, 19 May 2013 18:28:12 GMT
Server: WSGIServer/0.1 Python/2.7.4
Content-Length: 18
Content-Type: application/json
{"hello": "world"}
Run Code Online (Sandbox Code Playgroud) 我有一个我在本地开发的 Python 网络服务器(使用 Bottle 或 Flask 或任何其他):
BUILDVERSION = "0.0.128"
@route('/')
def homepage():
... # using a template, and the homepage shows the BUILDVERSION in the footer
# so that I always know which live version is running
...
run(host='0.0.0.0', port=8080)
Run Code Online (Sandbox Code Playgroud)
每次我有重大更新时,我都会:
git commit -am "Commit name" && git push
Run Code Online (Sandbox Code Playgroud)
并且更新了远程版本。(注意:我git config receive.denyCurrentBranch updateInstead在远程仓库上使用)。
问题:我经常忘记BUILDVERSION在每次提交时手动增加,然后就不容易区分哪个版本正在运行等(因为连续两次提交可能具有相同的BUILDVERSION!)
问题:有没有办法BUILDVERSION使用 Python + Git 在每次提交时自动递增?或任何类似的(BUILDVERSION 也可以是提交 ID...),它们将以小字符出现在网站的页脚中,允许区分 Python 代码的连续版本。
有没有办法限制执行GET时python请求将遵循的重定向数量?
我知道 allow_redirects=False,但这只是防止重定向一起.我正在寻找一种方法来跟踪重定向,最多可达到一些跳数.
# What I know how to do:
resp = requests.get(url) # follows redirects "infinitely"
resp = requests.get(url, allow_redirects=False) # follows no redirects
# What I'm trying to do:
resp = requests.get(url, max_redirects=3) # follow up to 3 redirects
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:虽然任何测试建议都受到真正的赞赏,但我特别想知道是否有一种方法 pytest 可以为我强制隔离,而不是依靠自己总是记得清理模拟。
是否pytest支持在各个 Python 文件之间“重置”进程状态,或者以其他方式将测试文件彼此隔离?
我们的 CI 用于在单个文件上调用 pytest,如下所示:
pytest file1.py
pytest file2.py
...
Run Code Online (Sandbox Code Playgroud)
现在,它对所有文件调用 pytest 一次,如下所示:
pytest file1.py file2.py ...
Run Code Online (Sandbox Code Playgroud)
file1.py当一些测试文件(例如)执行模块级模拟时,我们遇到了麻烦。例如(简化):
def setup_module():
patch("path.to.some.module.var", Mock()).start()
Run Code Online (Sandbox Code Playgroud)
(没有对应的teardown_module。)
当一次对文件调用 pytest 时,这种方法效果很好。但是,当它针对多个文件运行时,先前执行的测试代码所做的任何模拟都会保留到后续的测试代码中。有什么方法可以“重置”文件之间的这种状态吗?例如,pytest 是否支持在单独的进程中调用每个文件的测试的方法?(查看了 pytest 文档,但没有发现类似的内容。)
目前,我们正在添加teardown_module()调用 的函数patch.stopall(),但如果能够增加 pytest 的安全性来隐式地将我们的测试文件彼此隔离,那就太好了。
可以说我有
import peewee
class Foo(Model):
name = CharField()
Run Code Online (Sandbox Code Playgroud)
我想做以下事情:
f = {id:1, name:"bar"}
foo = Foo.create_from_dict(f)
Run Code Online (Sandbox Code Playgroud)
这是Peewee的本地人吗?我无法在源代码中发现任何内容.
我已经编写了这个函数,但是如果存在则宁可使用本机函数:
#clazz is a string for the name of the Model, i.e. 'Foo'
def model_from_dict(clazz, dictionary):
#convert the string into the actual model class
clazz = reduce(getattr, clazz.split("."), sys.modules[__name__])
model = clazz()
for key in dictionary.keys():
#set the attributes of the model
model.__dict__['_data'][key] = dictionary[key]
return model
Run Code Online (Sandbox Code Playgroud)
我有一个显示所有foos的网页,允许用户编辑它们.我希望能够将JSON字符串传递给控制器,在那里我将它转换为dict,然后从中制作Foos,因此我可以根据需要进行更新.
我试图找出我的用户正在使用哪些浏览器,但遇到了问题。如果我尝试阅读标题“ User-Agent”,通常会给我很多文本,并且什么也没告诉我。例如,如果我使用Chrome浏览器访问网站,则在“ User-Agent”标题中有:
用户代理:“ Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 31.0.1650.57 Safari / 537.36”。
如您所见,尽管提到了Mozzila,Safari,Chrome等,但它什么也没告诉我。
我一直在使用的框架是Bottle(Python)。
任何帮助,将不胜感激,谢谢。
python ×6
bottle ×1
git ×1
git-commit ×1
http ×1
http-headers ×1
mocking ×1
peewee ×1
pytest ×1
user-agent ×1