tchellomello / python-amcrest

A Python 2.7/3.x module for Amcrest and Dahua Cameras using the SDK HTTP API.
GNU General Public License v2.0
216 stars 76 forks source link

realtime_stream - 400 Client Error: Bad Request for url: <ip> #172

Open Moortiii opened 3 years ago

Moortiii commented 3 years ago

I'm trying to stream a realtime recording from an Amcrest IP4M camera via Wifi. The camera connects just fine, and can be controlled and take snapshots. When I try to connect using realtime_stream, the HTTP API throws 400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0.

I have tried changing the channel and sub-type accordingly, referencing the HTTP API Docs.

Minimal example:

from amcrest import AmcrestCamera

camera = AmcrestCamera('192.168.1.173', 80, <username>, <password>).camera
print("Information:", camera.software_information)
print("Video enabled:", camera.video_enabled)

camera.snapshot(0, "/home/user/Pictures/snapshot00.jpeg")
camera.realtime_stream(path_file="/home/user/Pictures/rt_stream_test")

Output:

Information: ('version=2.620.00AC000.3.R', 'build:2019-09-19\r\n')
Video enabled: True

Traceback:

<AMC047CF_625EBA:AMC047CF40F8625EBA> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0')
<AMC047CF_625EBA:AMC047CF40F8625EBA> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0')
<AMC047CF_625EBA:AMC047CF40F8625EBA> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0')
Traceback (most recent call last):
  File "/home/user/dev/embedded/venv/lib/python3.7/site-packages/amcrest/http.py", line 204, in _command
    resp.raise_for_status()
  File "/home/user/dev/embedded/venv/lib/python3.7/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    camera.realtime_stream(path_file="/home/user/Pictures/rt_stream_test",)
  File "/home/user/dev/embedded/venv/lib/python3.7/site-packages/amcrest/special.py", line 34, in realtime_stream
    channel, typeno), stream=True
  File "/home/user/dev/embedded/venv/lib/python3.7/site-packages/amcrest/http.py", line 169, in command
    return self._command(*args, **kwargs)
  File "/home/user/dev/embedded/venv/lib/python3.7/site-packages/amcrest/http.py", line 209, in _command
    raise CommError(error)
amcrest.exceptions.CommError: 400 Client Error: Bad Request for url: http://192.168.1.173:80/cgi-bin/realmonitor.cgi?action=getStream&channel=1&subtype=0
Moortiii commented 3 years ago

I inspected the connection attempt using Wireshark and discovered that it returns a 401 Unauthorized for an initial request towards /cgi-bin/magicBox.cgi?action=getMachineName. I can also confirm that connecting via the IP address directly in the browser allows me to see the camera stream in real-time using the same credentials as provided to python-amcrest.

jeffstearns commented 1 year ago

I see the same problem. I'm using the current version of python-amcrest with up-to-date Amcrest firmware. The camera works fine with a web browser, vlc, and Amcrest View software. It doesn't work with python-amcrest.

Note: I'm passing keyword args to camera.snapshot() to confirm to the current code which expects keyword arguments only.

>>> camera = AmcrestCamera('10.5.5.17', 80, <USERNAME>, <PASSWORD>).camera
>>> print("Information:", camera.software_information)
Information: ('2.820.00AC002.0.R', '2023-04-08\r\n')
>>> print("Video enabled:", camera.video_enabled)
Video enabled: True
>>> camera.snapshot(channel=0, path_file="/Users/jps/Desktop/snapshot00.jpeg")
<AMC06592CCA1190F65:AMC06592CCA1190F65> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://10.5.5.17:80/cgi-bin/snapshot.cgi?channel=0')
<AMC06592CCA1190F65:AMC06592CCA1190F65> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://10.5.5.17:80/cgi-bin/snapshot.cgi?channel=0')
<AMC06592CCA1190F65:AMC06592CCA1190F65> Trying again due to error: HTTPError('400 Client Error: Bad Request for url: http://10.5.5.17:80/cgi-bin/snapshot.cgi?channel=0')
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/amcrest/http.py", line 286, in _command
    resp.raise_for_status()
  File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://10.5.5.17:80/cgi-bin/snapshot.cgi?channel=0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/amcrest/snapshot.py", line 84, in snapshot
    ret = self.command(cmd, timeout_cmd=timeout, stream=stream)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/amcrest/http.py", line 222, in command
    return self._command(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/amcrest/http.py", line 295, in _command
    raise CommError(error) from error
amcrest.exceptions.CommError: 400 Client Error: Bad Request for url: http://10.5.5.17:80/cgi-bin/snapshot.cgi?channel=0