Browse Source

测试代码,用于模拟访问dcm服务器,看是否能得到dcm文件

dengdx 3 weeks ago
parent
commit
5cea414245
2 changed files with 58 additions and 0 deletions
  1. BIN
      py/DemoImage.dcm
  2. 58 0
      py/fetch_dcm.py

BIN
py/DemoImage.dcm


+ 58 - 0
py/fetch_dcm.py

@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+"""
+下载需要登录才能访问的 DICOM 文件
+"""
+
+import requests
+from pathlib import Path
+
+# =========== 按需修改 ===========
+USERNAME = 'admin'
+PASSWORD = '123456'
+# 如需忽略自签名证书,把 VERIFY 设成 False
+VERIFY   = True
+# ================================
+
+# BASE_URL = 'http://192.168.11.12:6001/dr/api/v1'
+BASE_URL = 'http://101.43.219.60:7700/dr/api/v1'
+
+login_url = f'{BASE_URL}/pub/login'
+file_url  = f'{BASE_URL}/auth/image/dcm/DemoImage.dcm'
+local_file = Path('DemoImage.dcm')
+
+sess = requests.Session()
+sess.headers.update({'User-Agent': 'python-requests/2.x'})
+
+# 1. 登录
+login_payload = {
+    'username': USERNAME,
+    'password': PASSWORD
+}
+resp = sess.post(login_url, json=login_payload, verify=VERIFY, timeout=10)
+print(resp.text)
+resp.raise_for_status()
+
+# 2. 先 HEAD 看文件信息
+
+head = sess.head(file_url)
+print('[HEAD]', head.status_code, head.headers)
+
+# 如果你的接口返回 {"token": "xxxx"},可以存下来备用
+print('登录成功,获取 token...')
+print(resp.json()['data']['token'])
+token = resp.json()['data']['token']
+sess.headers.update({'Authorization': f'Bearer {token}'})
+sess.headers.update({'Language': f'en'})
+sess.headers.update({'Product': f'DROS'})
+sess.headers.update({'Source': f'Electron'})
+
+# 2. 下载文件
+print('正在下载文件...')
+with sess.get(file_url, stream=True, verify=VERIFY, timeout=3000) as r:
+    r.raise_for_status()
+    with open(local_file, 'wb') as f:
+        for chunk in r.iter_content(chunk_size=8192):
+            if chunk:          # 过滤 keep-alive
+                f.write(chunk)
+
+print(f'下载完成:{local_file.absolute()}')