Django Web 应用程序和运行 python 脚本的外部服务器之间的通信
我目前正在使用Django在网页上工作。该网站旨在显示不同模拟的结果。我无法直接在Django后端中使用某些模拟的代码。但是代码所有者允许我使用自己的服务器进行黑匣子模拟。因此,这个想法是首先将带有数据的某种请求发送给服务器。数据是由用户早期输入的,并保存在Django数据库中。然后,服务器接收数据并使用此数据驱除模拟(Python脚本)。模拟完成后,数据将发送回我的Django后端,因此我的Django后端会收到某种响应。 我在这个主题上进行了很多研究,但找不到我可以使用的东西。我看到了诸如Python插座,socket.io,WebSockets,Django Rest框架和Django频道之类的东西,但是我并没有真正看到如何将这些库之一应用于我的情况。我如何实现前面描述的东西?如何将我的Django项目连接到远程服务器(在另一个城市中)?我从哪里开始?
我尝试使用python scockets实现服务器和客户端,但是这没有连接到Django。我研究了Django频道,但没有看到如何连接到远程服务器而不是许多客户端。
我试图找到一种设置服务器(在终端中)的方法,然后在Django中实现该服务器和我的后端之间的通信。
而且,如果这通常看起来像是一种不好的做法,我将开放有关如何在我的网站中脱离模拟而不直接访问仿真代码的建议。
I am currently working on a web page using Django. This website aims at displaying results of different simulations. I can not use the code of some simulation directly in my Django backend. But the code owners allow me to do a black box simulation using their own servers. So the idea is to first send some sort of request with data to the server. The data was inputed earlier by the user and saved in the django database. Then the server receives the data and excecutes a simulation (a python script) using this data. After the simulation is finished the data is sent back to my django backend, so my django backend receives some kind of response.
I researched a lot on this topic but could not really find something I could use. I saw stuff like python sockets, socket.io, websockets, django rest framework and django channels, but I did not really see how I could apply one of these libraries to my case. How do I achieve what a described earlier? How can I connect my django project to a remote server (in a different city)? Where do I start?
I tried to implement a server and client using python sockets but this had no connection to django. I looked into Django channels but did not see how I could connect to a remote server instead of many clients.
I tried to find a way to setup a server (in a terminal) and then implement a communication between this server and my backend in django simulating this scenario of the communication to an external server, but I could not find a method to do so yet.
And if this generally seems like a bad practice, I am open for suggestions on how to incooperate simulations in my website without direct access to the simulation code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以实现所需的目标,这就是我的做法。首先,仿真服务器需要以广泛接受的格式发布模拟结果,最好是json到eventbus。接下来,您的Django应用程序将收听总线中的事件,并在需要持久的情况下在DB中创建记录,否则只需使用REDIS将它们存储在缓存中。从那里,通过Websocket将模拟推向前端客户端并渲染它们。我正在附上一个小图,介绍该过程的外观。如果没有任何DevOps支持,则所有这些堆栈都无法由单个开发人员开发和维护,因此,将一些AWS托管服务用于公共汽车和Websockets将有用且便宜。
如果整个项目都与此有关,则甚至不需要Django,您可以使用圣杯。每当将新事件发布到总线上,可以将记录保存到DynamoDB并将其发布到AWS WebSockets,并且如果您需要以后或刷新相同的模拟,则会将记录保存到DynamoDB,并将其保存到AWS Websocket,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您可以开发另一个,但您可以开发另一个,请将记录保存到AWS WebSockets,您可以将记录保存到DynamoDB,并将记录保存到AWS Websockets,您可以将记录保存到DynamoDB,您可以将记录保存到DynamoDB,您lambda作为DynamoDB顶部的REST API。
您可以使用任何DB,而是使用DynamoDB,但是在您的情况下,这是最容易使用并随时可用的。
在两者之间使用EventBus的原因:
There are a lot of ways to achieve what you need and here's how I'd do it. First, the simulation server needs to publish the simulation result in a widely accepted format, preferably JSON to an eventbus. Next, your django app will listen to the events from the bus and creates the records in the db if they need to be persistent, else just store them in cache, using redis. From there, push the simulations via websockets to the frontend client and render them. I'm attaching a small diagram on how the process would look like. All this stack can be quite overwhelming to be developed and maintained by a single developer without any DevOPs support, so it would useful and cheap to use some of AWS managed services for both the bus and websockets.
If the whole project is about this, django is not even required, you can go with a serveless approach using chalice. A lambda function (written in python) triggered whenever a new event has been published to the bus, can save the records to dynamodb and also publish to the aws websockets and if you need to access later or upon refresh the same simulations you can develop another lambda as REST API on top of the dynamodb.
Instead of the dynamoDB you can use any db, but in your case this is the easiest to work with and readily available.
Reasons why to use an eventbus in between: