docker(85) dockerでpython2 Qwatch(TS-WLCAM)を動かしてみた

# 前提

dockerで機械学習している
https://qiita.com/kaizen_nagoya/items/ddd12477544bf5ba85e2

Pythonはdocker上で使っている。


## 仕事の仕方

仕様、案などは、常に直交する３つを考える。

優先順位、難しさ、時間的な違い

３つの案のうち、どれから着手するのが優先順位。

他の２つは、自分以外に振る。または優先順位が高いのが終わったら次の事項をする。

### まずそのままを動かす。

変更は、原則gitで。
変更する場合は、理由を書く。




## pythonで
「Qwatch(TS-WLCAM)ゲットしたから画像ぶっこ抜いてみた．」
という記事を拝見した。Qwatchが手元にあるので確かめた。

http://miettal.hatenablog.com/entry/2013/10/06/040434

## ソース
https://gist.github.com/miettal/6844563

mac OS Xでdocker for macでdockerを起動しています。

## docker login

```
$ docker login
Authenticating with existing credentials...
Stored credentials invalid or expired
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username : xxxxx
Password: yyyyy
Login Succeeded
```
## docker pull

```
$ docker pull kaizenjapan/anaconda2-willi 
Using default tag: latest
latest: Pulling from kaizenjapan/anaconda2-willi
05d1a5232b46: Pull complete 
b46ad780a34a: Pull complete 
543ba6ae39a1: Pull complete 
a617ad1c5335: Pull complete 
eaf64a68b9e4: Pull complete 
a7a9bbc4929a: Pull complete 
Digest: sha256:87bedeeae875023bd345be53b1e05a7c20dcc963fbda8eb52d77a2a7e4afadef
Status: Downloaded newer image for kaizenjapan/anaconda2-willi:latest
```
## docker run

```
$ docker run -it -p 80:80 -p 8888:8888  kaizenjapan/anaconda2-willi 

```
## git clone @ docker

```
(base) root@9722a3ce067b:/# git clone https://gist.github.com/miettal/6844563
Cloning into '6844563'...
remote: Enumerating objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (6/6), done.

(base) root@9722a3ce067b:/# ls
6844563					  boot	home   media  proc  sbin  tmp
BuildingMachineLearningSystemsWithPython  dev	lib    mnt    root  srv   usr
bin					  etc	lib64  opt    run   sys   var

(base) root@9722a3ce067b:/# cd 6844563/

(base) root@9722a3ce067b:/6844563# ls
grab_mjpeg_frame.py

(base) root@9722a3ce067b:/6844563# python grab_mjpeg_frame.py 192.158.2.85 80 /snapshot.cgi admin CAMA577 destfile.jpg 
Traceback (most recent call last):
  File "grab_mjpeg_frame.py", line 40, in <module>
    if not boundary:
NameError: name 'boundary' is not defined
```
python2の版

```
(base) root@9722a3ce067b:/6844563# python2 --version
Python 2.7.15 :: Anaconda, Inc
```
dockerではなく, Mac OSで

```
$ git clone https://gist.github.com/miettal/6844563
Cloning into '6844563'...
remote: Enumerating objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (6/6), done.

$ cd 6844563/

$ ls
grab_mjpeg_frame.py

$ python2 grab_mjpeg_frame.py 192.158.2.85 80 /snapshot.cgi admin CAMA577 destfile.jpg 
Traceback (most recent call last):
  File "grab_mjpeg_frame.py", line 40, in <module>
    if not boundary:
NameError: name 'boundary' is not defined
```
python2の版


```
$ python2 --version
Python 2.7.14
```

ファイル一覧見てもなにもできてない。

boundary = ''を10行目にいれると。

```
$ vim grab_mjpeg_frame.py

$ python2 grab_mjpeg_frame.py 192.158.2.85 80 /snapshot.cgi admin CAMA577 destfile.jpg 
Traceback (most recent call last):
  File "grab_mjpeg_frame.py", line 42, in <module>
    raise Exception("Can't find content-type")
Exception: Can't find content-type
```

boundary = ' 'と空白にすると、

10分経っても何も返答がない。macで別のコマンドで確認するが、ファイルもできていない。

boundary = ':'とセミコロンにしても、
10分経っても何も返答がない。

https://gist.github.com/goingmywaynet/e5caea360a1f87cd0fe1

のファイルを試す。
### mac OSの方。
mac OS の既定で入っているpython2はpython2とすれば動く。


```
$ vi GrabQwatchMJPEG.py

$ python2 GrabQwatchMJPEG.py 192.158.2.85 80 /snapshot.cgi admin CAMA577 destfile.jpg 
Traceback (most recent call last):
  File "GrabQwatchMJPEG.py", line 9, in <module>
    import requests                          # digest認証対応用
ImportError: No module named requests
```
### requests
が入っていないらしい

```
KM-S05:6844563 administrator$ pip2 install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
    100% |████████████████████████████████| 92kB 116kB/s 
Collecting idna<2.8,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 252kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/56/9d/1d02dd80bc4cd955f98980f28c5ee2200e1209292d5f9e9cc8d030d18655/certifi-2018.10.15-py2.py3-none-any.whl (146kB)
    100% |████████████████████████████████| 153kB 187kB/s 
Collecting urllib3<1.24,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 187kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 276kB/s 
Installing collected packages: idna, certifi, urllib3, chardet, requests
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python2.7/site-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python2.7/site-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/usr/local/lib/python2.7/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/idna'
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
KM-S05:6844563 administrator$ pip2 install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 492kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:

$ sudo pip2 install requests
Password:
The directory '/Users/administrator/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/administrator/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
  Downloading https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
    100% |████████████████████████████████| 92kB 38kB/s 
Collecting idna<2.8,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 118kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/56/9d/1d02dd80bc4cd955f98980f28c5ee2200e1209292d5f9e9cc8d030d18655/certifi-2018.10.15-py2.py3-none-any.whl (146kB)
    100% |████████████████████████████████| 153kB 27kB/s 
Collecting urllib3<1.24,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 165kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 112kB/s 
Installing collected packages: idna, certifi, urllib3, chardet, requests
Successfully installed certifi-2018.10.15 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
KM-S05:6844563 administrator$ sudo pip2 install --upgrade pip2
The directory '/Users/administrator/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/administrator/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip2
  Could not find a version that satisfies the requirement pip2 (from versions: )
No matching distribution found for pip2
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
KM-S05:6844563 administrator$ sudo pip2 install --upgrade pip
The directory '/Users/administrator/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/administrator/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 317kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-18.1

$ python2 GrabQwatchMJPEG.py '\http://192.168.2.85/snapshot.cgi\' admin CAMA577 output.jpg 
access to \http://192.168.2.85/snapshot.cgi\
Traceback (most recent call last):
  File "GrabQwatchMJPEG.py", line 26, in <module>
    r = requests.get(requestStr, auth=HTTPDigestAuth(userStr, passwordStr), stream = True) # Qwatch への接続 with Digest認証
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 616, in send
    adapter = self.get_adapter(url=request.url)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 707, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '\http://192.168.2.85/snapshot.cgi\'

```
docker の方では

```
(base) root@9722a3ce067b:/6844563# python GrabQwatchMJPEG.py '\http://192.168.2.85/snapshot.cgi\' admin CAMA577 output.jpg 
access to \http://192.168.2.85/snapshot.cgi\
Traceback (most recent call last):
  File "GrabQwatchMJPEG.py", line 26, in <module>
    r = requests.get(requestStr, auth=HTTPDigestAuth(userStr, passwordStr), stream = True) # Qwatch への接続 with Digest認証
  File "/opt/conda/lib/python2.7/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/opt/conda/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/conda/lib/python2.7/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/conda/lib/python2.7/site-packages/requests/sessions.py", line 616, in send
    adapter = self.get_adapter(url=request.url)
  File "/opt/conda/lib/python2.7/site-packages/requests/sessions.py", line 707, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '\http://192.168.2.85/snapshot.cgi\'
```

同じエラーになったが嬉しくない。

ブラウザで
http://192.168.2.85/snapshot.cgi
とするとファイルが一つ取れる。


```
$ docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                                        NAMES
9722a3ce067b        kaizenjapan/anaconda2-willi   "/usr/bin/tini -- /b…"   2 hours ago         Up 2 hours          0.0.0.0:80->80/tcp, 0.0.0.0:8888->8888/tcp   focused_spence
KM-S05:6844563 administrator$ docker commit 9722a3ce067b  kaizenjapan/anaconda2
sha256:51b530fbdfb17e275cb0b53d5d0c741758558b3525c082098814b4b0ac780f81
KM-S05:6844563 administrator$ docker push kaizenjapan/anaconda2
The push refers to repository [docker.io/kaizenjapan/anaconda2]
6773a5f64bc3: Pushed 
0c4a9d6af7b0: Mounted from kaizenjapan/anaconda2-willi 
9bd66f1ffc09: Mounted from kaizenjapan/anaconda2-willi 
5048be0c0a05: Mounted from kaizenjapan/anaconda2-willi 
1ecb0ee91526: Mounted from kaizenjapan/anaconda2-willi 
ab39eed3db29: Mounted from kaizenjapan/anaconda2-willi 
b28ef0b6fef8: Mounted from kaizenjapan/anaconda2-willi 
latest: digest: sha256:4386632abc4c076ff201fa949f0a1bc9574c23c6eca1f365f8d971fe1ed11280 size: 1800
```

```
$ python2 GrabQwatchMJPEG.py 'http://192.168.2.85/snapshot.cgi' admin CAMA577 output.jpg 
access to http://192.168.2.85/snapshot.cgi
output_201810161433.jpg
saved
KM-S05:6844563 administrator$ ls
GrabQwatchMJPEG.py	grab_mjpeg_frame.py	grab_mjpeg_frame2.py	output_201810161433.jpg
```
![output_201810161433.jpg](https://qiita-image-store.s3.amazonaws.com/0/51423/644da5ec-71f1-b9c3-69e7-976bcd8bdb7b.jpeg)

取れた！

なんじゃ、バックスラッシュがいらなんだ。

### 最後までおよみいただきありがとうございました。

いいね　💚、フォローをお願いします。
#### Thank you very much for reading to the last sentence.

Please press the like icon 💚　and follow me for your happy life.