import requests

# 目标请求地址
url = 'http://183.220.37.46:23424/file_parse'

# 请求头
headers = {
    'accept': 'application/json'
}

# 构建表单数据和文件
files = {
    # 各种表单参数
    'return_middle_json': (None, 'false'),
    'return_model_output': (None, 'false'),
    'return_md': (None, 'true'),
    'return_images': (None, 'false'),
    'end_page_id': (None, '99999'),
    'parse_method': (None, 'auto'),
    'start_page_id': (None, '0'),
    'lang_list': (None, 'ch'),
    'output_dir': (None, './output'),
    'server_url': (None, 'string'),
    'return_content_list': (None, 'false'),
    'backend': (None, 'hybrid-auto-engine'),
    'table_enable': (None, 'true'),
    'response_format_zip': (None, 'false'),
    'formula_enable': (None, 'true'),
    # 文件上传字段（核心）
    'files': open(r'/D:/wx_work/sichuan_luqiao/lu_sgsc_testfile/施工方案/301_四川川交路桥有限责任公司秦唐高速公路唐山段ZT1合同项目部.pdf', 'rb')
}

try:
    # 发送 POST 请求
    response = requests.post(
        url=url,
        headers=headers,
        files=files,
        # 设置超时时间，避免请求一直挂起
        timeout=600
    )
    
    # 打印响应结果
    print(f"响应状态码: {response.status_code}")
    print(f"响应内容: {response.json()}")

except FileNotFoundError as e:
    print(f"错误：文件未找到 - {e}")
except requests.exceptions.Timeout as e:
    print(f"错误：请求超时 - {e}")
except requests.exceptions.RequestException as e:
    print(f"错误：请求失败 - {e}")
except ValueError as e:
    print(f"错误：响应不是有效的JSON - {e}")
    print(f"原始响应内容: {response.text}")
finally:
    # 确保文件句柄关闭
    if 'files' in locals() and hasattr(files['files'], 'close'):
        files['files'].close()