我有一台在Mac OS X主机上运行Ubuntu 12.04的VirtualBox机器.在我的主机(Mac OS X)中,我安装了PostgreSQL.我想从我的Ubuntu虚拟机连接到这个PostgreSQL服务器(我通常知道它是相反的方式).
我想我应该在VirtualBox本身配置一些网络参数.我从Vagrant文档获得的是我需要为我的虚拟机分配一个静态IP以使用专用网络.但是一旦创建,我如何从我的客人那里联系我的主人?
在流浪文档中我没有找到类似的东西.所以,这可能是一个很好的理由.是否有意义?我不想复制PostgreSQL安装.只需使用我现有的那个.
我正在尝试使用Docker容器来运行PostgreSQL服务器,并从我的主机连接它.
我的配置是:
我这样做了:
第1步:为永久性postgres数据创建一个卷
docker volume create --name postgres_data
Run Code Online (Sandbox Code Playgroud)
第2步:启动postgres实例
更新:正如评论中所建议的那样,我在运行容器时指定了端口映射
docker run --name my_postgres_container -e POSTGRES_PASSWORD=my_password -v postgres_data:/var/lib/postgresql/data -p 5432:5432 -d postgres:9.1
Run Code Online (Sandbox Code Playgroud)
第3步:通过执行以下操作连接到Docker实例:
docker run -it --link my_postgres_container:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
Run Code Online (Sandbox Code Playgroud)
但我想通过以下方式连接到该实例:
psql -h localhost -p 5432 -U postgres
Run Code Online (Sandbox Code Playgroud)
就像我在我的主机上本地安装了Postgres一样.
问题是端口5432没有暴露.所以,我无法连接它:
sudo lsof -i -P | grep -i "listen" - >没有端口5432打开
更新2:更奇怪.我也这样做了:
停止Docker.然后,在我的主机上运行一个普通的PostgreSQL 9.4.4实例(此处不涉及docker,只需在我的Mac OS …
好吧,这是2016年的WebPack看起来像对一个胜利者requirejs和browserify.我一直在阅读这3种技术,以解决一个非常具体的问题.我希望在我的HTML文件中避免这种情况(AngularJS应用程序的一部分)
<script src="some-file.js"></script>
<script src="some-file2.js"></script>
<script src="some-file3.js"></script>
<!-- Dozens of similar lines here... -->
Run Code Online (Sandbox Code Playgroud)
当然,我的HTML文件中这些行的顺序很重要.Bootstrap将要求jQuery等.
我找到的第一件事是:requirejs.你只需指定这样的东西:
<script src="my-bundled-file.js"></script>
Run Code Online (Sandbox Code Playgroud)
然后,您使用JS解决依赖性问题.更进一步,我在这里发现了两种方法:
Webpack适用于这两种方法,听起来不错.
最后,3个工具可以用于同一件事:只在一个文件中捆绑几个文件.但我担心的是这些文件捆绑的顺序.
我不想关心这个,看起来像使用这些解决方案(甚至gulp + gulp-concat,就像这里建议的那样),我只是在解决问题:现在,我指定我的应用程序使用的模块JS代码,但我仍然需要以正确的顺序放置模块,即使使用WebPack(这里的示例:require调用必须按正确顺序)
所以,我的问题:
在创建WSGIApplication实例时,有没有办法将参数传递给RequestHandler对象?
我的意思是
app = webapp2.WSGIApplication([
('/', MainHandler),
('/route1', Handler1),
('/route2', Handler2)
], debug=True)
Run Code Online (Sandbox Code Playgroud)
是否可以传递一些参数MainHandler,Handler1或Handler2?
提前致谢
我正在使用AngularJS创建一个Web应用程序.为了测试它,我使用angular-seed模板在NodeJS服务器中运行应用程序.
在这个应用程序中,我需要通过POST请求向另一个主机发送JSON消息,并获得响应,因此,我正在使用CORS.
我的请求是通过实现使用AngularJS http服务的服务完成的(我需要$ http提供的抽象级别.所以,我不使用$ resource).
在这里,我的代码.请注意我修改$ httpProvider以告诉AngularJS使用适当的CORS头发送其请求的事实.
angular.module('myapp.services', []).
// Enable AngularJS to send its requests with the appropriate CORS headers
// globally for the whole app:
config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
/**
* Just setting useXDomain to true is not enough. AJAX request are also
* send with the X-Requested-With header, which indicate them as being
* AJAX. Removing the header …Run Code Online (Sandbox Code Playgroud) 我正在尝试对我在基于 Django 的应用程序的管理员中使用的Django 表单之一进行单元测试。该表单的 Meta 类有一个模型字段。不确定哪种方法更好:
我更喜欢第二种方法(我认为这种方法是我在浏览器中填写表单时使用的方法,对吗?),但我不知道如何表示 ManyToMany 字段
在这里,一些(简化的)代码:
一、车型
class Segment(models.Model):
name = models.CharField(max_length=150)
app_name = models.CharField(max_length=150)
class ProceduralDocument(models.Model):
name = models.CharField(max_length=150)
content = models.TextField(null=True, blank=True, default=None)
deleted = models.BooleanField(default=False)
date_added = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
segments = models.ManyToManyField(Segment)
Run Code Online (Sandbox Code Playgroud)
如您所见,ProceduralDocument 中只有 2 个必填字段:
现在,管理员
class ProceduralDocumentAdminForm(forms.ModelForm):
class Meta:
model = ProceduralDocument
widgets = {'content': RedactorEditor(redactor_options={
'lang': 'es',
'redactor_css': "styles/pydocx_styles.css"
}), }
exclude = ['deleted']
def …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的课程
class MyClass(AnotherClass):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
def f(self):
#dostuff
Run Code Online (Sandbox Code Playgroud)
我有字符串"我的班级"
str = "my class"
Run Code Online (Sandbox Code Playgroud)
我可以做这个:
class_name_from_str = str.title().replace(" ", "") # now class_name_from_str is "MyClass"
Run Code Online (Sandbox Code Playgroud)
我怎么能让class_name_from_str可调用?我想要这样的东西:
callable_obj = some_process(class_name_from_str)
o = callable_obj(arg1, arg2)
o.f()
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我正在使用Python 2.7
更新:正如Matti Virkkunen在评论中建议的那样,一个好的解决方案是创建一个dict来映射字符串和类
my_calls_dict = {'my class' : MyClass, 'my other class' : MyOtherClass}
Run Code Online (Sandbox Code Playgroud) 我正在用Python开发一个Google App Engine应用程序.我正在使用:
我以为我必须使用服务帐户,因为这样:
"如果您的App Engine应用程序需要调用API来访问应用程序项目拥有的数据,则可以使用服务帐户简化OAuth 2.0"
摘自https://developers.google.com/api-client-library/python/platforms/google_app_engine#ServiceAccounts
但我不确定我是否误解了某些东西.我的方案(GAE应用尝试在我自己的域中访问Google Apps)是服务帐户的候选人吗?
我已经尝试了几种方法来处理OAuth2:
在这两种情况下,我都会遇到同样的错误:
我完全迷失了.有线索吗?
提前谢谢了
python google-app-engine google-calendar-api oauth-2.0 google-api-python-client
我想获取两个给定日期之间我的 Google 日历的所有空闲/忙碌事件。我正在关注freebusy 对象的文档。
\n\n基本上,我有一个index.html,其表单允许选择两个日期。我将这些日期发送到我的应用程序(Python Google AppEngine 支持)。
\n\n这是经过简化的代码,使其更具可读性:
\n\nCLIENT_SECRETS = os.path.join(os.path.dirname(__file__), \'client_secrets.json\')\n\ndecorator = oauth2decorator_from_clientsecrets(\n CLIENT_SECRETS,\n scope=\'https://www.googleapis.com/auth/calendar\',\n message=MISSING_CLIENT_SECRETS_MESSAGE)\n\nservice = build(\'calendar\', \'v3\')\n\nclass MainPage(webapp2.RequestHandler):\n @decorator.oauth_required\n def get(self):\n # index.html contains a form that calls my_form\n template = jinja_enviroment.get_template("index.html")\n self.response.out.write(template.render())\n\nclass MyRequestHandler(webapp2.RequestHandler):\n @decorator.oauth_aware\n def post(self):\n if decorator.has_credentials():\n\n # time_min and time_max are fetched from form, and processed to make them\n # rfc3339 compliant\n time_min = some_process(self.request.get(time_min))\n time_max = some_process(self.request.get(time_max))\n\n #\xc2\xa0Construct freebusy query request\'s body\n freebusy_query = {\n "timeMin" : …Run Code Online (Sandbox Code Playgroud) 我在我的app.yaml配置文件中使用'login'选项来获取GAE应用程序.看起来像这样:
- url: /admin/.*
script: myapp.app
login: admin
- url: /.*
script: myapp.app
login: required
Run Code Online (Sandbox Code Playgroud)
更新(通过bossylobster的建议):我希望用户始终登录(未签名的用户无法做任何事情),我需要知道用户是谁.实际上,我需要OAuth2凭据才能与Google API进行通信(例如,我需要使用Google个人资料API获取一些用户信息,并使用Google Calendar API在用户的日历中写入).最后,我需要管理员用户执行某些操作(例如使用Google Provisioning API创建新域用户)
我正在使用google-api-client库,并且使用oauth2装饰器.然后,在我的RequestHandlers中,我有这个:
class MainHandler(webapp.RequestHandler):
@decorator.oauth_aware
def get(self):
if decorator.has_credentials():
# do something
else:
url = decorator.authorize_url()
self.response.out.write(template.render('templates/index.html',
{'authorize_url': url}))
Run Code Online (Sandbox Code Playgroud)
最后,我读到了另一种方法:
user = users.get_current_user()
if user:
# do something
else:
greeting = ("<a href=\"%s\">Sign in or register</a>." %
users.create_login_url("/"))
self.response.out.write("<html><body>%s</body></html>" % greeting)
Run Code Online (Sandbox Code Playgroud)
处理用户身份验证以满足我的需求的最佳方法是什么(请参阅更新)?
提前谢谢了
google-app-engine oauth-2.0 python-2.7 app.yaml google-api-python-client
我想检查是否装饰了Python函数,并将decorator参数存储在函数dict中。这是我的代码:
from functools import wraps
def applies_to(segment="all"):
def applies(func):
@wraps(func)
def wrapper(*args, **kwargs):
func.func_dict["segment"] = segment
print func.__name__
print func.func_dict
return func(*args, **kwargs)
return wrapper
return applies
Run Code Online (Sandbox Code Playgroud)
但是看起来该字典丢失了:
@applies_to(segment="mysegment")
def foo():
print "Some function"
> foo() # --> Ok, I get the expected result
foo
{'segment': 'mysegment'}
> foo.__dict__ # --> Here I get empty result. Why is the dict empty?
{}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个与POCO C++库动态链接的C++库.我广泛使用POCO来做几件事,比如套接字,文件处理,日志记录等.所以,我需要处理POCO可以抛出的异常.
除了与POCO相关的异常之外,我的代码可以抛出其他异常,原因有几个(基本上是RunTime异常).POCO C++实际上包含一个RunTimeException类.所以,我可以使用它.
我的问题是:我是否应该依赖POCO异常,并允许使用我的库的第三方直接捕获它们?另一种选择是创建我自己的异常集,包装POCO异常,并公开它们.这样,如果我决定将来摆脱POCO,我不需要改变那部分.只是我的包装异常.
还有其他没有明显的理由用我自己的POCO例外包装所有的POCO例外吗?
提前谢谢了.
python-2.7 ×4
oauth-2.0 ×3
python ×3
javascript ×2
postgresql ×2
angularjs ×1
app.yaml ×1
browserify ×1
c++ ×1
callable ×1
cors ×1
django-1.8 ×1
django-forms ×1
docker ×1
jquery ×1
networking ×1
node.js ×1
requirejs ×1
ubuntu-12.04 ×1
unit-testing ×1
vagrant ×1
virtualbox ×1
webapp2 ×1
webpack ×1
wrapper ×1