如何在本地 django 服务器上设置 SSL 来测试 Facebook 应用程序?

发布于 2024-12-07 13:02:19 字数 316 浏览 1 评论 0原文

我已将本地计算机的 HOSTS 配置配置为每当我点击 http://www 时就可以访问本地服务器 (@ 127.0.0.1)。 mydomain.com 在浏览器上。

我使用它与 facebook 的图形 api 交互来构建我的应用程序。但现在 facebook 要求我们有一个 HTTPS url 或者更确切地说是 SSL 安全的 url 来与他们的 api 交互。

所以问题是-> 如何在本地 django 服务器上设置 SSL?

I've configured my local machine's HOSTS configuration to access the local server ( @ 127.0.0.1 ) whenever I hit http://www.mydomain.com on the browser.

And I was using this to interact with facebook's graph api to build my app. But now facebook requires us to have an HTTPS url or rather an SSL secured url to interact with their api.

So the question is -> How do I setup SSL on a local django server ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

凡尘雨 2024-12-14 13:02:19

不是为了破坏线程,但我发现这个工具非常容易使用。

它是一个预制的 Django 应用程序,具有非常简单的安装说明。

安装后,您只需运行以下命令即可添加经过认证的密钥:

python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key

我希望这对可能看到此内容的任何路人有所帮助。

Not to necro a thread, but I found this tool to be extremely easy to use.

It's a premade django application with very simple install instructions.

You can add a certified key once it is installed simply by running:

python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key

I hope this helps any passer-by who might see this.

月寒剑心 2024-12-14 13:02:19

使用django-extensions,您可以运行以下命令:

python manage.py runserver_plus --cert certname

如果它不存在,它将自动生成一个(自签名)证书。几乎太简单了。

您只需要安装以下依赖项:

pip install django-extensions
pip install Werkzeug
pip install pyOpenSSL

现在, 正如 Ryan Pergent 在评论中指出的,您最后只需要添加'django_extensions', 添加到您的 INSTALLED_APPS 中,应该可以正常使用了。

我之前使用过隧道,它有效,但这更容易,并且附带许多其他命令。

With django-extensions you can run the following command:

python manage.py runserver_plus --cert certname

It will generate a (self-signed) certificate automatically if it doesn't exist. Almost too simple.

You just need to install the following dependencies:

pip install django-extensions
pip install Werkzeug
pip install pyOpenSSL

Now, as Ryan Pergent pointed out in the comments, you lastly only need to add 'django_extensions', to your INSTALLED_APPS and should be good to go.

I used a tunnel before, which worked, but this is much easier and comes with many other commands.

旧情别恋 2024-12-14 13:02:19

简而言之,您需要在开发计算机上设置适当的网络服务器。使用您最熟悉的任何一种(Apache、nginx、cherokee 等)。

更长的答案是,django 开发服务器 (manage.py runserver) 并不是为执行 SSL 等而设计的,并且使其执行此操作的工作量可能比您想要花费的要多。

请参阅 django 用户列表中对此 passim 的讨论:http://groups.google.com/group/django-users/browse_thread/thread/9164126f70cebcbc/f4050f6c82fe1423?lnk=gst&q=ssl+development+server#f4050f6c82fe1423

Short answer is you'll need to setup a proper webserver on your development machine. Use whichever one (Apache, nginx, cherokee etc) you're most familiar with.

Longer answer is that the django development server (manage.py runserver) isn't designed to do SSL etc and the effort to make it do so is likely greater than you'd want to spend.

See discussions of this passim on the django-users list: http://groups.google.com/group/django-users/browse_thread/thread/9164126f70cebcbc/f4050f6c82fe1423?lnk=gst&q=ssl+development+server#f4050f6c82fe1423

挽清梦 2024-12-14 13:02:19

在 django 上运行 https 的解决方法。

这可以通过 stunnel 来完成,让 Facebook 服务器和您计算机上的 stunnel 以 SSL 进行通信,然后 stunnel 反过来以 HTTP 方式与 Python 进行通信。首先安装stunnel。例如在 Mac OS X 中:

brew install stunnel

然后您需要创建一个设置文件以供 stunnel 执行。您可以在任何地方创建文本文件。例如,您可以创建 dev_https 并输入:

pid=
cert=/usr/local/etc/stunnel/stunnel.pem
foreground=yes
debug=7

[https]
accept=8001
connect=8002
TIMEOUTclose=1

stunnel 创建一个假证书。默认情况下,在 Mac 上,它位于 /usr/local/etc/stunnel/stunnel.pem。它会在您的浏览器上发出警告,提示您的网页可能是假的,但 Facebook 操作仍然正常。由于 stunnel 必须监听一个端口,而 Python 的开发服务器无法在同一台服务器上运行,因此您必须使用不同的端口来接受(传入)和连接(内部)。一旦您拥有 dev_https 文件或任何您所称的文件,请运行

sudo stunnel dev_https

以启动隧道。然后启动你的 Python 服务器。

HTTPS=1 python manage.py runserver 0.0.0.0:8002

环境变量 HTTPS 必须设置为 1 才能返回安全响应,并且由于我们之前将内部端口设置为 8002,因此我们侦听来自所有传入 IP 的 8002。然后,您的 IP:8001 可以接受 HTTPS 连接,而无需更改您的 Web 服务器,并且您可以继续在不同端口上运行 HTTP Python 服务器的另一个实例。

参考:
https://medium.com/xster-tech/django-开发服务器与 https-103b2ceef893

Workaround to run https on django.

This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:

brew install stunnel

Then you need to create a settings file for stunnel to execute. You can create a text file anywhere. For instance, you can create dev_https and input:

pid=
cert=/usr/local/etc/stunnel/stunnel.pem
foreground=yes
debug=7

[https]
accept=8001
connect=8002
TIMEOUTclose=1

stunnel creates a fake certificate. By default on Mac, it’s at /usr/local/etc/stunnel/stunnel.pem. It’ll bring up a warning on your browser saying that your webpage can be fake but Facebook operations still work right. Since stunnel has to listen on one port and Python’s development server cannot run on the same server, you must use different ports for accept (incoming) and connect (internal). Once you have your dev_https file or whatever you called it, run

sudo stunnel dev_https

to start the tunnelling. Then start your Python server.

HTTPS=1 python manage.py runserver 0.0.0.0:8002

Environment variable HTTPS must be set to 1 for it to return secure responses and since we previously set the internal port to 8002, we listen on 8002 from all incoming IPs. Then, your IP:8001 can accept HTTPS connections without changing your webserver and you can continue running another instance of HTTP Python server on a different port.

ref:
https://medium.com/xster-tech/django-development-server-with-https-103b2ceef893

疑心病 2024-12-14 13:02:19

我知道这个问题已经得到解答,但为了更清晰的解决方案:

第 1 步: 安装库

pip install django-sslserver

第 2 步: 添加到 settings.py 中已安装的应用程序

INSTALLED_APPS = [
    'sslserver',
    '...'
]

第 3 步: 使用runsslserver 而不是runserver 运行代码。证书及键是可选的。

python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key

I understand this has already been answered, but for a clearer solution:

Step 1: Install library

pip install django-sslserver

Step 2: Add to installed apps in settings.py

INSTALLED_APPS = [
    'sslserver',
    '...'
]

Step 3: Run the code using runsslserver instead of runserver. Certificate & key are optional.

python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key
溇涏 2024-12-14 13:02:19

这并不能通过

./manage.py test

解决自动测试问题,但要使用 HTTPS 运行服务器,您可以使用 RunServerPlus: http://pythonhosted.org/django-extensions/runserver_plus.html

只需安装 django-extensions 和 pyOpenSSL:

pip install django-extensions pyOpenSSL

然后运行:

python manage.py runserver_plus --cert cert

This doesn't solve the automatic testing issue via

./manage.py test

but to run a server with HTTPS you can use RunServerPlus: http://pythonhosted.org/django-extensions/runserver_plus.html

Just install django-extensions and pyOpenSSL:

pip install django-extensions pyOpenSSL

and then run:

python manage.py runserver_plus --cert cert

╭ゆ眷念 2024-12-14 13:02:19

我已经能够使用 stunnel 在 django 的测试服务器上设置 ssl。 这里是有关如何设置它的一些

信息请注意,我无法使用 apt-get 中 debian 提供的软件包让它工作,我必须从源代码安装。如果您必须这样做,请查看出色的说明 debian 论坛
网上和 stunnel FAQ 上有很多关于如何创建 pem 证书的说明,但最终 Debian 上的 dpkg-buildpackage 为我构建了它。

我想在 Windows 上事情实际上可以更直接。

然后,我可以通过在“调试配置”下添加 HTTPS=1 环境变量,使 eclipse 中的 pydev 启动测试服务器(并附加到它) -> 「环境」->变量

I've been able to setup ssl on django's test server by using stunnel. Here is some info on how to set it up

Just a note, I wasn't able to get it working using the package provided by debian in apt-get and I had to install from source. In case you have to do the same, please check out the excellent instructions debian forums on how to build debian packages.
There are plenty of instructions online and also on stunnel FAQ on how to create your pem certificate, but ultimately dpkg-buildpackage on Debian built it for me.

I would imagine that things could actually be more straight forward on Windows.

I then was able to make pydev in eclipse start the test server (and also attach to it) by adding a HTTPS=1 environment variable under "Debug Configurations" -> "Environment" -> Variables

恍梦境° 2024-12-14 13:02:19

当我想测试使用 Facebook 注册时,我遇到了同样的问题。使用 https://github.com/teddziuba/django-sslserver 中的 django SSL 服务器后。这个问题就解决了。您可能也需要它。

I got the same problem when wanna test Sign up using Facebook. After use django SSL Server from https://github.com/teddziuba/django-sslserver. This problem is solved. You may need it too.

网名女生简单气质 2024-12-14 13:02:19

这个讨论页面确实很老了,早期的Django不支持SSL,需要通过stunnel或Werkzeug来完成。
Django 现在支持 django-sslserver 的 SSL 配置:
https://djangopackages.org/packages/p/django-sslserver/
添加安装应用程序并在命令行中传递证书。

This discussion page is really old, earlier Django does not supported SSL, it needs to be done through stunnel or Werkzeug.
Django now supports SSL configuration with django-sslserver:
https://djangopackages.org/packages/p/django-sslserver/
Add in install app and pass certs in command line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文