支持REST样式的JSON over HTTP访问的文档数据库似乎是支持AJAX丰富的应用程序的理想选择,其中浏览器直接调用数据库,绕过传统的Web服务器/应用程序逻辑组件.例如,一旦用户通过身份验证,就可以检索用户首选项.(BBC主页可能是一个很好的例子,在负载崩溃之前!)
这种情况的问题是安全问题 - 如果用户使用Web服务器进行身份验证(例如基本表单身份验证),该身份如何转移到文档DB.是通过Web服务器代理对数据库的所有请求的唯一答案 - 即保护文档数据库,以便没有直接的外部访问?
这似乎是最有意义的,也是最容易实现的,但我想知道是否有人在异构环境中使用文档dbs有经验和/或建议?
我有一台运行Ubuntu 12.04的虚拟机,我正在尝试安装ElasticSearch.我按照这个要点中最好的"学习X硬盘的方式"的精神,一切精细安装-包下载,未解压,复制到正确的地方等
当我运行它时会出现问题 - 通过调用:
$ /usr/local/share/elasticsearch/bin/elasticsearch
Run Code Online (Sandbox Code Playgroud)
或者使用服务包装器($ rselasticsearch console)
输出将记录到wrapper.log下面并包含在内.我认为我可能有JAVA家庭/类路径问题,但我不确定.
最感激的任何帮助!
Running ElasticSearch...
wrapper | Unable to write to the configured log directory: /usr/local/share/elasticsearch/logs (No such file or directory)
wrapper | The directory does not exist.
wrapper | Unable to write to the configured log file: /usr/local/share/elasticsearch/logs/service.log (No such file or directory)
wrapper | Falling back to the default file in the current working directory: wrapper.log
wrapper | --> Wrapper Started as Console …Run Code Online (Sandbox Code Playgroud) 以下代码适用于Python交互式shell:
import urllib2
result = urllib2.urlopen("http://www.google.com/")
Run Code Online (Sandbox Code Playgroud)
并给出200结果.
如果我在与开发服务器本地运行的AppEngine应用程序中运行相同的代码,则会因以下错误而失败:
URLError: <urlopen error An error occured while connecting to the server:
Unable to fetch URL: http://www.google.com/
Error: [Errno 11004] getaddrinfo failed>`
Run Code Online (Sandbox Code Playgroud)
我试过urlfetch直接使用这个库:
from google.appengine.api import urlfetch
result = urlfetch.fetch("http://www.google.com")
Run Code Online (Sandbox Code Playgroud)
这也失败了(这是有道理的,因为我相信urllib2内部的AppEngine调用URLFetch?)
我可以清楚地从我的本地机器访问URL - 所以发生了什么?
更新:相关的堆栈跟踪:
File "c:\dev\repos\stackoverflow\main.py", line 40, in get_latest_comments
result = urlfetch.fetch("http://www.google.com")
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 266, in fetch
return rpc.get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 604, in get_result
return self.__get_result_hook(self)
File "C:\Program Files …Run Code Online (Sandbox Code Playgroud) 我有一个Postgres安装程序,设置为使用LATIN1作为默认编码。但是,我的生产数据库要求我使用UTF8(它托管在Heroku上,因此我别无选择)。
我创建了本地开发数据库以正确设置此设置:
sudo createdb -Upostgres $PROJECT_NAME --template=template0 \
--encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8
Run Code Online (Sandbox Code Playgroud)
但是,我现在无法运行django测试,因为测试数据库仅使用默认的Postgres集群设置(LATIN1),这会导致测试失败(某些模板中有无效字符- ...character 0xe28099 of encoding "UTF8" has no equivalent in "LATIN1")
'nuclear'选项是使用正确的(en_US.UTF8)设置重新安装Postgres,但是由于我是在VM中运行的,因此每次启动VM时,我都并不需要这样做。如果首先有一种方法可以按摩Django正确创建数据库,那将是更好的选择。
[更新1:TEST_ENCODING]
遵循@sneawo的建议,我设置了数据库的TEST_ENCODING属性,现在出现以下错误:
Creating test database for alias 'default'...
Got an error creating the test database: encoding UTF8 does not match locale en_US
DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1.
Run Code Online (Sandbox Code Playgroud)
[更新2:核选项]
我在上面提到了这一点,但它并不适合所有人,但是由于我是在VM上运行它,因此对于我来说很容易在Vagrant设置脚本(shell脚本)中使用正确的排序规则重新创建postgres集群:
sudo service postgresql stop
sudo pg_dropcluster 9.1 --stop main
sudo pg_createcluster --start -e UTF-8 9.1 main
sudo cp -f $CONF_DIR/pg_hba.conf /etc/postgresql/9.1/main/pg_hba.conf
sudo cp -f …Run Code Online (Sandbox Code Playgroud) 我有一个元组列表,如下所示:
[
(1, "red")
(1, "red,green")
(1, "green,blue")
(2, "green")
(2, "yellow,blue")
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试汇总数据,以便获得以下dict输出:
{
1: ["red", "green", "blue"]
2: ["green", "yellow", "blue"]
}
Run Code Online (Sandbox Code Playgroud)
注意事项是:将颜色字符串组合为主键(数字),然后分成列表,然后进行去重复(例如使用set)。
我也想做反演,并按颜色分组:
{
"red": [1],
"green": [1, 2]
"yellow": [2]
"blue": [1, 2]
}
Run Code Online (Sandbox Code Playgroud)
我可以通过遍历所有元组来清楚地做到这一点,但我想尝试使用list / dict理解来做到这一点。
我有一组测试依赖于使用 python 模拟库和@mock.patch装饰器模拟日期,以及在此处找到的日期模拟代码示例。使用这个,我们有一个 FakeDate 类:
class FakeDate(original_date):
"A fake replacement for datetime.date that can be mocked for testing."
def __new__(cls, *args, **kwargs):
return original_date.__new__(original_date, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
在我们的测试中,我们有:
from datetime import date as real_date
@mock.patch('datetime.date', FakeDate)
def test_mondays_since_date(self):
FakeDate.today = classmethod(lambda cls: real_date(2014, 1, 1)) # A Wednesday
self.assertNotEqual(datetime.date.today(), real_date.today())
self.assertEqual(datetime.date.today().year, 2014)
# and so on..
Run Code Online (Sandbox Code Playgroud)
一切都在工作,直到我将 Django 从 1.4.8 升级到 1.5.5。不幸的是,现在模拟日期导致测试失败,但仅限于模型保存操作。堆栈跟踪如下:
File "/site-packages/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)
File "/site-packages/django/db/models/base.py", line 650, in save_base
result …Run Code Online (Sandbox Code Playgroud) (按照Heroku自己的支持指示在这里问)
我们刚刚在dev环境之间的库不匹配的项目中发现了依赖性问题.细节是不相关的,但根本原因是一个依赖项,其中有一个"> ="版本匹配setup.py- 这意味着当一个开发人员重建他的环境时,他突然得到了最新版本(0.4.0)而不是旧版本他以前的版本(0.3.11),并开始得到一个DeprecationWarning.
作为调试过程的一部分,我的印象是每当将一个仓库推送到Heroku时,就会重建一个干净的环境,这导致我错误地假设我们的DEV环境(每天重建)会有最新的版本安装.因为我们没有在开发环境中看到问题,所以我决定进行调查,并heroku run pip list在远程环境中运行.
我非常惊讶地看到,这是旧的和过期的依赖项的幸运下降,而根本不是一个干净的环境.事实证明,作为旧安装的一部分,我们可能遇到了我们正在调试我们的实时环境中幸福生活的问题.
最简单的解释方法是BeautifulSoup库.我们最近从v3更新到v4,作为其中的一部分,库本身将PyPI上的名称从更改BeautifulSoup为beautifulsoup4.我们更新了requirements.txt以反映这一点,但如果我现在运行pip list我们的Heroku环境,我得到两个:
~ $ heroku run bash
~ $ pip list
BeautifulSoup (3.2.1)
beautifulsoup4 (4.3.2)
Run Code Online (Sandbox Code Playgroud)
因此,旧的依赖性尚未被清除,它只是坐在那里.通过启动python会话我可以很容易地证明它:
~ $ python
Python 2.7.4 (default, Apr 6 2013, 22:14:13)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
>>> import BeautifulSoup
>>>
Run Code Online (Sandbox Code Playgroud)
这有点令人震惊,令我惊讶的是,这还没有在某些时候杀死我们的应用程序?
所以,问题是 - Heroku如何在引擎盖下管理依赖关系 - 它显然不会擦除python环境并pip …
我试图在我的本地开发环境(运行Ubuntu 12.04的vagrant VM)上设置ElasticSearch/Haystack,我无法解决重新索引过程.
ES正在运行,我已经创建了一个新索引(我使用elasticsearch-head来查看浏览器中的索引状态).我可以创建一个新索引并查询它,所以我知道ES正在工作.
我的问题是Haystack rebuild_index命令:
(.venv)vagrant@precise32:/app$ foreman run ./manage.py rebuild_index
WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
DEBUG Making a request equivalent to this: curl -XDELETE 'http://127.0.0.1:9200/test_app' -d '""'
INFO Starting new HTTP connection (1): 127.0.0.1 …Run Code Online (Sandbox Code Playgroud) 我有一个字符串元组,我想通过使用分隔符拆分每个元素来转换为字典.词典理解能实现这一点吗?
>>> x = ('var1=abc', 'var2=xyz', 'var3=10')
>>> y = {k: v for (k, v) in ???} # this is where I need your help
>>> print y
{ "var1": "abc", "var2": "xyz", "var3": "10" }
Run Code Online (Sandbox Code Playgroud)
这与任何事情一样都是学习练习 - 所以请不要用另一种方式回答(我有很多这样做) - 我想知道是否可以使用词典理解.
[UPDATE]
我没有先写下一些细微的修改 - 我需要从每个值的末尾删除一个无关的换行符.
>>> x = ('var1=abc\n', 'var2=xyz\n', 'var3=10')
>>> d = {k: v.strip('\n') for (k, v) in [e.split('=') for e in x]}
>>> d
{'var1': 'abc', 'var3': '10', 'var2': 'xyz'}
Run Code Online (Sandbox Code Playgroud)
(当然,现在它的顺序并不相同 - 但我可以忍受.Grrr.)
我正在尝试从包含wheel档案的本地包目录中安装一些python要求。我正在Docker容器中安装需求。
我要执行的步骤是:
$ pip install wheel
# wheel runs, outputs .whl files to wheelhouse directory
$ pip wheel --wheel-dir wheelhouse -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
然后,在我的Dockerfile:
ADD requirements.txt /tmp/requirements.txt
ADD wheelhouse /tmp/wheelhouse
# install requirements. Leave file in /tmp for now - may be useful.
RUN pip install --use-wheel --no-index --find-link /tmp/wheelhouse/ -r /tmp/requirements.txt
Run Code Online (Sandbox Code Playgroud)
这行得通-正确安装了所有要求:
# 'app' is the name of my built docker image
$ docker run app pip list
...
psycopg2 (2.5.1)
...
Run Code Online (Sandbox Code Playgroud)
但是,如果我真的尝试运行的容器里面的东西的用途 psycopg2,然后我得到如下: …