Skip to content

Commit b204407

Browse files
author
ictar
committed
add translation for "Import a Docker Container in Python"
1 parent 46d214c commit b204407

6 files changed

Lines changed: 990 additions & 1 deletion

‎Django/1.9/ref/中间件.md‎

Lines changed: 392 additions & 0 deletions
Large diffs are not rendered by default.

‎Others/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@
2626

2727
- [为部署Python web应用程序构建一个更好的用户体验](./为部署Python web应用程序构建一个更好的用户体验.md)
2828

29-
介绍使用warpdrive简单快捷的在不同的WSGI上部署web应用以及一些关于改进部署体验的做法
29+
介绍使用warpdrive简单快捷的在不同的WSGI上部署web应用以及一些关于改进部署体验的做法
30+
31+
- [在Python中导入一个Docker容器](./在Python中导入一个Docker容器.md)
32+
33+
介绍了一个工具Sidomo,它可以将奇怪的软件转换成在Python代码中无缝运行的漂亮且单纯的Python模块。
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
原文:[Import a Docker Container in Python](http://blog.deepgram.com/import-a-docker-container-in-python/)
2+
3+
---
4+
5+
### 为什么要这样做?
6+
7+
Docker容器对于应用之间彼此隔离是非常棒的,但如果你想要它们之间彼此通信呢?例如,如果你正在用Python开发一个应用,而这个应用需要与其他语言编写的软件进行交互。有[一些技巧](https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages)可以用来实现Python与其他流行语言之间低级别的互操作。但是如果你处于一种[奇怪的情况](http://stackoverflow.com/questions/546160/what-is-erlang-written-in?answertab=votes#tab-top),或使用一些复杂的传统软件,这将变得困难,甚至是不可能的。
8+
9+
### 想法:作为模块的容器
10+
11+
我们创建了[sidomo - 简单的Docker模块](https://github.com/deepgram/sidomo)*,这样,如果你让你奇怪的应用程序在任何linux环境上运行,那么你可以立即以零添加的形式从Python中调用它。
12+
13+
现在,大多数人使用Docker Daemon API来管理执行他们应用程序的容器。([Kubernetes](http://kubernetes.io/) / [Mesos](http://mesos.apache.org/)是这方面很好的例子)。Sidomo为容器开辟了一个全新的用例 —— 将奇怪的软件转换成在Python代码中无缝运行的漂亮且单纯的Python模块。
14+
15+
*并不是一个[AWS服务](https://www.expeditedssl.com/aws-in-plain-english)
16+
17+
## 如何使用sidomo
18+
19+
请确保你安装了Docker,并且运行了一个Docker守护进程。如果你不确定是否是这样,那么运行`docker ps`,然后看看是否获得"CONTAINER ID ..."输出。如果你不确定如何正确设置Docker,那么你可以看看[这个链接](https://docs.docker.com/engine/installation/)或者[搜索这里](https://www.google.com/search?q=install+docker)来查找对应的方法。
20+
21+
### 设置Sidomo: 单行方式
22+
23+
你可以使用pip直接从git仓库中安装sidomo。只需在你的shell中运行下面这个命令:
24+
```sh
25+
pip install -e 'git+https://github.com/deepgram/sidomo.git#egg=sidomo'
26+
```
27+
28+
### 例子:一个简单的Hello World
29+
30+
这将从Ubuntu基本映像中启动一个容器,运行`echo hello from`,然后运行`echo the other side`,然后从该过程中打印输出行。要为这个例子做准备,你需要使用一个shell命令将Ubuntu映像拉到你的机器上。
31+
32+
###### shell
33+
```sh
34+
# Get the latest Ubuntu image
35+
docker pull ubuntu
36+
```
37+
38+
###### Python
39+
```py
40+
from sidomo import Container
41+
42+
with Container('ubuntu') as c:
43+
for line in c.run('bash -c "echo hello from; echo the other side;"'):
44+
print(line)
45+
```
46+
47+
### 例子:使用sidomo处理FFMPEG
48+
49+
现在,让我们用sidomo实际做一些有用的东西。[FFMPEG](https://www.ffmpeg.org/)是一个比较复杂的软件,对于大多数用途,它可以有效地操纵媒体文件,但是,它不容易在不同平台上进行一致的安装,并且没有最新的Python绑定它。使用Sidomo,你可以用Docker拉取FFMPEG,并且轻松地从Python运行它。
50+
51+
###### shell
52+
```py
53+
docker pull cellofellow/ffmpeg
54+
```
55+
56+
###### Python
57+
58+
下面的例子将从URL中抓取音频,对其进行转码,并打印调试信息来证明其有效。该进程的标准输出(原始音频输出)被禁用了,因为我们只希望看到调试信息。
59+
```py
60+
from sidomo import Container
61+
url = 'http://www2.warwick.ac.uk/fac/soc/sociology/staff/sfuller/media/audio/9_minutes_on_epistemology.mp3'
62+
with Container(
63+
'cellofellow/ffmpeg',
64+
stdout=False
65+
) as c:
66+
for line in c.run(
67+
'bash -c \"\
68+
wget -nv -O tmp.unconverted %s;\
69+
ffmpeg -i tmp.unconverted -f wav -acodec pcm_s16le -ac 1 -ar 16000 tmp.wav;\
70+
cat tmp.wav\
71+
\"\
72+
' % url
73+
):
74+
print line
75+
```
76+
77+
如果你确实想在这个进程中保存转码后的音频,那么你可以用`stderr=False`替换`stdout=False`行,然后确保将容器进程中的每一行输出(原始音频数据)写到一个文件中。
78+
79+
## 乐享未来
80+
81+
如果你必须为一些复杂的软件编写Python绑定,那么可以考虑容器化软件来代替。使用sidomo把一个容器化的应用程序转换成一个Python模块是不费劲的,并且是干净的。
82+
83+
如果你发现对于那些不存在合适的绑定的进程,你自己经常使用子进程与代码进行交互,那么容器化这些过程可能会让一些事情变得更简单。
84+
85+
![](http://www.adweek.com/socialtimes/files/2014/01/twitter-nesting-dolls.jpg)
86+
87+
如果你在这样的一个Python应用中使用sidomo —— 它以开发了复杂依赖告终 —— 那么你可能需要将它包装在自己的容器中,并在外面从一个具有较少依赖的应用程序调用它。Sidomo也支持这样做,因为[docker支持嵌套容器](https://blog.docker.com/2013/09/docker-can-now-run-within-docker/)。你可以通过使用sidomo导入sidomo导入sidomo来做自己的软件俄罗斯套娃....
88+
89+
祝你好运!只要记住,你不能无限期地容器化复杂度。或者,你可以?
90+
91+
[在github上的Sidomo](https://github.com/deepgram/sidomo)
92+
93+
## 为什么我们做这个?
94+
95+
我们创建了DeepGram API,一个用于音频和视频的搜索引擎,它使得语音可搜索。DeepGram使用一个信号处理的复杂堆栈,统计和机器学习的软件协同工作,以提供一个无缝的“上传和搜索”体验。Sidomo让我们迅速地容器化挑剔的软件,并将其与Python,我们的胶水,整合在一起。
96+
97+
你可以在[www.deepgram.com](http://www.deepgram.com)上获得一个带有API访问权限的帐户。该帐户每月可以进行40小时免费上传(这个很长的路要走!)。来看看吧,让我们知道[你在想些什么](http://www.deepgram.com/contact)。
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
原文:[Hacking the Worcester Wave thermostat in Python – Part 3](http://blog.rtwilson.com/hacking-the-worcester-wave-thermostat-in-python-part-3/)
2+
3+
---
4+
5+
So, [last time](http://blog.rtwilson.com/hacking-the-worcester-wave-thermostat-in-python-part-2/) we worked out how communications were encrypted and managed to read the current status of the heating system (whether the boiler is on or not, the current temperature, and so on). That’s great – but it’d be even better if we could actually control the thermostat from Python: set the temperature, change from timer mode to manual mode etc. That’s what we’re going to focus on today.
6+
7+
So, using the same ‘man-in-the-middle’ approach I monitored the communications from the app as I changed various settings. When changing the temperature I got a message like this:
8+
9+
`<message id="lj8Vq-31" to="rrcgateway_458921440@wa2-mz36-qrmzh6.bosch.de" type="chat"><subject>/heatingCircuits/hc1/manualTempOverride/temperature</subject><body>PUT /heatingCircuits/hc1/manualTempOverride/temperature HTTP:/1.0\nContent-Type: application/json\nContent-Length: 25\nUser-Agent: NefitEasy\n\n\n\nXmuIR7wCfDZpPrPkrb/CqQ==\n</body></message>`
10+
11+
Again, tidying it up a bit, and removing the XMPP headers we see the body is:
12+
```py
13+
PUT /heatingCircuits/hc1/manualTempOverride/temperature
14+
15+
HTTP:/1.0
16+
17+
Content-Type: application/json
18+
19+
Content-Length: 25
20+
21+
User-Agent: NefitEasy
22+
23+
XmuIR7wCfDZpPrPkrb/CqQ==
24+
```
25+
If we decode the text at the bottom we find that it decodes as:
26+
27+
`<span class="s1">{"value":16}\x00\x00\x00\x00</span>`
28+
29+
This looks like JSON, with a bit of null-padding at the end (presumably so that the encryption routine is given data with the length divisible by a certain number) – and it makes sense, as I set the thermostat to 16 degrees.
30+
31+
So, if we send this message (but with a different number) then presumably the temperature setting will change?&nbsp;Well…sort of!
32+
33+
You see, changing the temperature depends on what mode you are in. There are two modes, and the UMD part of the status message tells you which one you are in: _manual_ or _clock._ If you’re in manual mode then you can change the temperature by simply sending a PUT message (like the one above) to&nbsp;`/heatingCircuits/hc1/temperatureRoomManual`, with JSON of&nbsp;`{"value":21}` (or whatever temperature you want).
34+
35+
However, if you’re in clock mode then you have to set the ‘override temperature’ (a PUT message to&nbsp;`/heatingCircuits/hc1/manualTempOverride/temperature`) with the same JSON as above,&nbsp;_and_ you then have to turn the ‘temperature override function’ on (a PUT message to&nbsp;`/heatingCircuits/hc1/manualTempOverride/status` with the JSON `{"value": "on"}`).
36+
37+
Oh, and if you want to change the mode then you can just send a PUT message to&nbsp;`/heatingCircuits/hc1/usermode` with the JSON `{"value": "clock"}` or `{"value":"manual"}`.
38+
39+
You may be wondering whether you get a response from these messages or not: you do, but they’re not very interesting. Unless there has been an error, all you get is:
40+
```py
41+
No Content
42+
43+
Content-Type: application/json
44+
45+
connection: close
46+
```
47+
48+
There are loads of other messages you can send to do all sorts of complicated things (such as changing the timer programme), but I haven’t bothered to investigate&nbsp;any of those yet. I know they will use the same format as these messages, they’ll just have slightly more complicated JSON payloads, and may require the sending of multiple messages. I’m just happy that I can read the status of my thermostat, and control basic settings (mode and temperature)!
49+
50+
So, I haven’t actually mentioned Python that much in this series so far (sorry!) – although, in fact, most of my ‘trial and error’ work was done through Python using the great [sleekxmpp library](http://sleekxmpp.com/). I have to confess here that I haven’t written the code as well as I should have done: I should really have designed it to implement a proper Finite State Machine, and send and receive the appropriate messages at the appropriate times, all while updating internal information in the Python classes…but I didn’t! Sorry – dealing with all of that, working asynchronously, was too much like hard work.
51+
52+
So, I wrote a&nbsp;`BaseWaveMessageBot` class that implemented connecting, sending messages, encoding and decoding message payloads and a bit of simple error handling. That class has all of the complicated stuff in it, so I then wrote a couple of very simple classes (`StatusBot` and `SetBot`) that send the appropriate messages and process the responses. I then combined these in a nice class called `WaveThermo`. Currently there aren’t many methods in WaveThermo, but I will gradually add more functionality as I need it.
53+
54+
The code is available [on Github](https://github.com/robintw/pywavethermo)&nbsp;and is fairly easy to use:
55+
56+
57+
Of course, I’ve only tested it with my thermostat – so if it doesn’t work for you then please let me know!
58+
59+
So, that’s it for this time – next time I’ll talk about a bit of the work I’ve done with automated monitoring of the temperature and the thermostat state, and some of the interesting patterns I’ve found.

0 commit comments

Comments
 (0)