12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/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()}')
|