Django和根进程 [英] Django and root processes

查看:288
本文介绍了Django和根进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Django项目中,我需要使用ICMP ping来检查LAN上的主机是否正常。我发现这个SO问题,它解答了如何在Python中ping一些东西,这个SO问题链接到资源解释如何使用sodoers文件。

In my Django project I need to be able to check whether a host on the LAN is up using an ICMP ping. I found this SO question which answers how to ping something in Python and this SO question which links to resources explaining how to use the sodoers file.

A 设备 model存储了局域网主机的IP地址,并且在向数据库添加新的设备实例之后(通过自定义视图而不是管理员)I设想检查设备是否响应使用AJAX调用API的API来显示该功能。

A Device model stores an IP address for a host on the LAN, and after adding a new Device instance to the DB (via a custom view, not the admin) I envisage checking to see if the device responds to a ping using an AJAX call to an API which exposes the capability.

但是(从第一个SO问题中建议的库的docstring)请注意,ICMP消息只能从以root身份运行的进程发送。

However (from the docstring of a library suggested in the the first SO question) "Note that ICMP messages can only be sent from processes running as root."

我不想以root用户身份运行Django,因为这是不好的做法。但是,这部分进程(发送和ICMP ping)需要以root身份运行。如果使用Django视图,我希望发送一个ping数据包来测试一个主机的活动,那么Django本身就需要以root身份运行,因为这是调用ping的过程。

I don't want to run Django as the root user, since it is bad practice. However this part of the process (sending and ICMP ping) needs to run as root. If with a Django view I wish to send off a ping packet to test the liveness of a host then Django itself is required to be running as root since that is the process which would be invoking the ping.

这些是我可以想到的解决方案,而我的问题是有更好的方法来执行选择的部分作为根的Django项目,除了以下内容:

These are the solutions I can think of, and my question is are there any better ways to only execute select parts of a Django project as root, other than these:


  1. 以root身份运行Django(请不要!)

  2. 将一个ping请求放在另一个进程的队列中 - 以root身份运行 - 可以定期检查和实现。也许像芹菜

  1. Run Django as root (please no!)
  2. Put a "ping request" in a queue that another processes -- run as root -- can periodically check and fulfil. Maybe something like celery.



有没有一个更简单的方法?



我想要像Django运行为root库,这可能吗?

Is there not a simpler way?

I want something like a "Django run as root" library, is this possible?

推荐答案

绝对没有办法,不要以root身份运行Django代码!

Absolutely no way, do not run the Django code as root!

我将以root身份运行一个守护进程(用Python编写,为什么不这样做),然后 IPC 。只要您确定验证内容并正确处理(例如,使用数组等使用 subprocess.call ),并且只传递数据(而不是要执行的命令)这应该是正常的。

I would run a daemon as root (written in Python, why not) and then IPC between the Django instance and your daemon. As long as you're sure to validate the content and properly handle it (e.g. use subprocess.call with an array etc) and only pass in data (not commands to execute) it should be fine.

这里是一个客户端和服务器的示例,使用web.py

Here is an example client and server, using web.py

服务器: a href =http://gist.github.com/788639 =nofollow> http://gist.github.com/788639
客户端: http://gist.github.com/788658

Server: http://gist.github.com/788639 Client: http://gist.github.com/788658

您将需要安装webpy.org,但值得一试。如果您可以将IP(或主机名)硬连接到服务器中并删除参数,那么更好。

You'll need to install webpy.org but it's worth having around anyway. If you can hard-wire the IP (or hostname) into the server and remove the argument, all the better.

这篇关于Django和根进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆