我正在尝试集成一个第三方应用程序,使用python-request来获取它从模板解析的urls。
我正在尝试使用LiveServerTestCase来测试集成。奇怪的是,curl可以工作,但请求测试test_requests_static_file失败,出现以下错误:
requests.exceptions.HTTPError: 502 Server Error: Connection refused for url: http://localhost:35819/static/testapp/style.css这里有什么想法吗?
import subprocess
import requests
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
class LiveServerTests(StaticLiveServerTestCase):
def test_curl_static_file(self):
output = subprocess.check_output(["curl", '%s%s' % (self.live_server_url, '/static/testapp/style.css')])
self.assertIn('background: blue', output)
def test_requests_static_file(self):
response = requests.get('%s%s' % (self.live_server_url, '/static/testapp/style.css'))
response.raise_for_status()发布于 2021-06-05 04:45:36
我正要删除这个问题,但我想它可能会对一些可怜的人有用。事实证明,此问题是由于尝试使用网络代理连接到本地主机的请求造成的。
通过此测试验证:
from requests.utils import should_bypass_proxies as requests_should_bypass_proxies
class LiveServerTests(StaticLiveServerTestCase):
def test_requests_should_bypass_proxies_for_liveserver(self):
self.assertTrue(requests_should_bypass_proxies(self.live_server_url, None))解决方案是使用NO_PROXY环境变量。如下效果的东西将会起作用:
import os
os.environ['NO_PROXY'] = 'localhost'https://stackoverflow.com/questions/67841789
复制相似问题