简体   繁体   English

如何使用Django保存到远程服务器

[英]How to save to a remote server with Django

I'm fairly new to Python and Django. 我是Python和Django的新手。 I've recently got a Django app working on localhost in Linux Mint 18.3 with a Postgresql database. 我最近有一个Django应用程序在Linux Mint 18.3中使用Postgresql数据库在localhost上工作。

I've uploaded the app to PythonAnywhere, but unfortunately it requires Java for some of the NLP features (Stanford POS). 我已将应用程序上传到PythonAnywhere,但不幸的是,它需要Java来实现一些NLP功能(Stanford POS)。 Is there a way to parse and process the data on a local system and save it to the remote Postgres DB and serving the data from the remote server? 有没有办法解析和处理本地系统上的数据并将其保存到远程Postgres DB并从远程服务器提供数据?

I've looked at some answers on here regarding SSH tunnelling, but I'm not sure this applies here? 我在这里看了一些关于SSH隧道的答案,但我不确定这适用于此? Alternatively would it be possible to save to the local database and periodically migrate the data to the remote database? 或者可以保存到本地数据库并定期将数据迁移到远程数据库吗?

Yes -- to access your PythonAnywhere Postgres DB from your local machine, you'll need to use SSH tunnelling. 是 - 要从本地计算机访问PythonAnywhere Postgres DB,您需要使用SSH隧道。 If you're using a Unix-like operating system (eg. Linux or Mac) on your local machine, the "Manual SSH tunnelling" instructions at the bottom of this help page will handle that, with a couple of tweaks. 如果您在本地计算机上使用类Unix操作系统(例如Linux或Mac),则此帮助页面底部的“手动SSH隧道”指令将通过一些调整来处理。

The SSH command on the help page is: 帮助页面上的SSH命令是:

ssh -L 3306:username.mysql.pythonanywhere-services.com:3306 username@ssh.pythonanywhere.com

...which, as you can see, is for MySQL. ......正如你所看到的,它适用于MySQL。 To make it work for your PythonAnywhere Postgres server, replace: 要使其适用于您的PythonAnywhere Postgres服务器,请替换:

  • The first 3306 with 5432 (which means that on your local machine, it will use the default Postgres port) 第一个33065432 (这意味着在本地机器上,它将使用默认的Postgres端口)
  • The username.mysql.pythonanywhere-services.com with the Postgres server hostname from the "Postgres" tab on the "Databases" page inside PythonAnywhere. username.mysql.pythonanywhere-services.com其中包含来自PythonAnywhere内“数据库”页面上“Postgres”选项卡的Postgres服务器主机名。
  • The second 3306 with the port from the "Postgres" tab on the "Databases" page inside PythonAnywhere. 第二个3306带有来自PythonAnywhere内“数据库”页面上“Postgres”选项卡的端口。

So you'll wind up with something like 所以你最终会结束这样的事情

ssh -L 5432:username-123.postgres.pythonanywhere-services.com:10123 username@ssh.pythonanywhere.com

...with different values for username , 123 and and 10123 . ... username12310123值不同。

When you run that command (and entered your PythonAnywhere login password, which it will prompt you for), a process will start up on your machine that looks like a Postgres server to all local processes, but is actually just forwarding everything back and forth to the PythonAnywhere-hosted database server. 当你运行该命令(并输入你将提示你的PythonAnywhere登录密码)时,一个进程将在你的机器上启动,看起来像Postgres服务器到所有本地进程,但实际上只是来回转发所有内容PythonAnywhere托管的数据库服务器。 So you can run your parsing and processing code locally, and it will work transparently. 因此,您可以在本地运行解析和处理代码,它将以透明方式工作。

Yes you just have to connect your local env to your remote DB using Django DATABASES settings https://docs.djangoproject.com/en/2.0/ref/settings/#databases 是的,您只需使用Django DATABASES设置将本地环境连接到远程数据库https://docs.djangoproject.com/en/2.0/ref/settings/#databases

Be careful to use your Django version in the doc (here 2.0) 小心在doc中使用你的Django版本(这里是2.0)

That way you can write from your local env, and read from your remote server (using the same settings to connect to your remote DB) 这样你就可以从本地env写入,并从远程服务器读取(使用相同的设置连接到远程数据库)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django-访问并将文件保存到远程服务器 - Django - Access and save files to remote server 如何将远程SQL Server与Django 2连接 - How to connect remote SQL server with Django 2 如何使用Windows对服务器进行Django远程访问 - How to Django remote access to server using Windows 如何有效地将数据保存到远程服务器数据库(python) - How to efficiently save data to a remote server database (python) 如何在Django中访问远程PostgreSQL数据库服务器(只读事务)? - How to access a remote PostgreSQL database server (Read only transaction) in Django? 远程服务器上的Django教程:如何在浏览器中查看? - Django tutorial on remote server: how to view in my browser? 如何在 django 模板中显示来自 sftp 远程服务器的图像文件? - How to display image file from sftp remote server in django template? 在Django上,如何通过SSH连接到远程服务器上的MySQL数据库? - On Django, how to connect to MySQL database on remote server through SSH? 如何捕获SMTPRecipientsRefused异常(使用远程SMTP服务器和django)? - How to catch the SMTPRecipientsRefused exception (using a remote SMTP server and django)? Django - 使用REMOTE_USER进行身份验证 - 如何在开发服务器中进行测试? - Django - Authentication using REMOTE_USER - How to test in dev server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM