如何合规调用https://stocks.exchange/api2/ticker接口解决SSL错误?
It looks like the issue stems from Python 2.7's outdated SSL stack not being able to negotiate a secure connection with the stocks.exchange server. Browsers use modern SSL libraries that support the protocols and ciphers the server requires, but Python 2.7's default setup doesn't. Here are your options to resolve this:
Solution 1: Upgrade to Python 3.x (Highly Recommended)
Python 2.7 is no longer supported, and its SSL libraries are obsolete. Upgrading to Python 3.x is the most reliable long-term fix. Here's a Python 3-compatible version of your code (with a browser-like User-Agent header to mimic browser requests, which some servers require):
import requests def get_ticker(): url = "https://stocks.exchange/api2/ticker" # Mimic a browser request to avoid being blocked headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } response = requests.get(url, headers=headers) return response.json() print(get_ticker())
Solution 2: Fix for Python 2.7 (If Upgrade Isn't Possible)
If you must stick with Python 2.7, you'll need to update your requests library and add better SSL support. Follow these steps:
- First, install required packages to enhance SSL compatibility:
pip install --upgrade requests pyopenssl ndg-httpsclient pyasn1
- Use a custom SSL adapter to force modern TLS protocols and compatible ciphers:
import json import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.poolmanager import PoolManager import ssl class SSLAdapter(HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=False): # Create an SSL context that uses modern protocols ctx = ssl.create_default_context() ctx.set_ciphers('DEFAULT@SECLEVEL=1') # Disable old, insecure protocols ctx.options |= ssl.OP_NO_SSLv2 ctx.options |= ssl.OP_NO_SSLv3 ctx.options |= ssl.OP_NO_TLSv1 ctx.options |= ssl.OP_NO_TLSv1_1 self.poolmanager = PoolManager( num_pools=connections, maxsize=maxsize, block=block, ssl_version=ssl.PROTOCOL_TLSv1_2, ssl_context=ctx ) def tick(): session = requests.Session() # Mount the custom adapter to handle HTTPS requests session.mount('https://', SSLAdapter()) # Add browser-like headers headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } r = session.get('https://stocks.exchange/api2/ticker', headers=headers) return r.json() print(tick())
Solution 3: Insecure Workaround (Not Recommended)
If the above fixes don't work, you could skip SSL verification, but this exposes you to man-in-the-middle attacks. Only use this in a fully trusted environment:
import requests def tick(): headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } r = requests.get('https://stocks.exchange/api2/ticker', headers=headers, verify=False) return r.json() print(tick())
内容的提问来源于stack exchange,提问作者RoachNut

