使用Python模拟Latex转图片API请求遭拒绝的问题排查
模拟LaTeX转图片API请求时连接被服务器拒绝的问题
我在模拟一个将LaTeX代码转换为图片的API请求,浏览器中发送的请求完全正常,但用Python模拟时服务器拒绝连接,求排查解决。
浏览器中正常的请求信息
POST /default/latex2image HTTP/2 Host: e1kf0882p7.execute-api.us-east-1.amazonaws.com Content-Length: 96 Sec-Ch-Ua: " Not A;Brand";v="99", "Chromium";v="104" Accept: application/json, text/javascript, */*; q=0.01 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Sec-Ch-Ua-Mobile: ?0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Sec-Ch-Ua-Platform: "Windows" Origin: https://latex2image.joeraut.com Sec-Fetch-Site: cross-site Sec-Fetch-Mode: cors Sec-Fetch-Dest: empty Referer: https://latex2image.joeraut.com/ Accept-Encoding: gzip, deflate Accept-Language: es-ES,es;q=0.9 {"latexInput":"\\begin{align*}\ {1}\ \\end{align*}\ ", "outputFormat":"PNG", "outputScale":"125%"}
我的Python代码
pload = { "latexInput":"{0}", "outputFormat":"PNG", "outputScale":"125%" } header = { "Content-Length": "96", "Sec-Ch-Ua": "Not A;Brand;v=99, Chromium;v=104", "Accept": "application/json, text/javascript, */*; q=0.01", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Sec-Ch-Ua-Mobile":"?0", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36", "Sec-Ch-Ua-Platform": "Windows", "Origin": "https://latex2image.joeraut.com", "Sec-Fetch-Site": "cross-site", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Dest": "empty", "Referer": "https://latex2image.joeraut.com/", "Accept-Encoding": "gzip, deflate", "Accept-Language": "es-ES,es;q=0.9", } r = requests.post("http://e1kf0882p7.execute-api.us-east-1.amazonaws.com", data=pload, headers=header) print(r.text) print(r.status_code)
运行后出现的错误
requests.exceptions.ConnectionError: HTTPConnectionPool(host='e1kf0882p7.execute-api.us-east-1.amazonaws.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000259A4D20670>: Failed to establish a new connection: [WinError 10061] No se puede establecer una conexión ya que el equipo de destino denegó expresamente dicha conexión'))
问题排查与解决
1. 请求URL错误
浏览器请求的完整路径是/default/latex2image,且使用HTTPS协议(HTTP/2),但你的代码仅指定了域名,未添加路径,且用了HTTP协议。AWS API Gateway默认只开放HTTPS端口443,HTTP端口80通常关闭,这是连接被拒绝的核心原因。
修正后的URL应为:https://e1kf0882p7.execute-api.us-east-1.amazonaws.com/default/latex2image
2. 手动设置Content-Length无效且易出错
你手动指定的Content-Length: 96和实际请求体长度不匹配时,服务器会因校验失败拒绝请求。requests库会自动计算并设置该字段,直接删除这个头即可。
3. 请求体格式不匹配
浏览器发送的是JSON格式请求体,但你用data=pload发送的是表单编码格式,和请求头描述存在矛盾。应使用json=pload发送JSON格式请求,同时将Content-Type改为application/json。
修正后的代码
import requests pload = { "latexInput": "\\begin{align*}\n{1}\n\\end{align*}\n", "outputFormat": "PNG", "outputScale": "125%" } header = { "Sec-Ch-Ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"104\"", "Accept": "application/json, text/javascript, */*; q=0.01", "Content-Type": "application/json", "Sec-Ch-Ua-Mobile": "?0", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36", "Sec-Ch-Ua-Platform": "\"Windows\"", "Origin": "https://latex2image.joeraut.com", "Sec-Fetch-Site": "cross-site", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Dest": "empty", "Referer": "https://latex2image.joeraut.com/", "Accept-Encoding": "gzip, deflate", "Accept-Language": "es-ES,es;q=0.9", } r = requests.post("https://e1kf0882p7.execute-api.us-east-1.amazonaws.com/default/latex2image", json=pload, headers=header) print(r.text) print(r.status_code)
内容的提问来源于stack exchange,提问作者David pociello quadrado
相关产品推荐
相关产品推荐

