Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bit_login/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Connection': 'close',
'Pragma': 'no-cache',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
Expand Down
25 changes: 24 additions & 1 deletion bit_login/login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from requests.adapters import HTTPAdapter
import re
from typing import Dict, Any
from .config import CONFIG
Expand All @@ -8,12 +9,34 @@ class login_error(Exception):
"""BIT登录通用异常"""
pass

class _TimeoutHTTPAdapter(HTTPAdapter):
"""HTTPAdapter that enforces a default request timeout."""
def __init__(self, *args, **kwargs):
self._default_timeout = kwargs.pop('timeout', 30)
super().__init__(*args, **kwargs)

def send(self, request, **kwargs):
kwargs.setdefault('timeout', self._default_timeout)
return super().send(request, **kwargs)

class login:
"""BIT 统一身份认证核心类"""
def __init__(self, base_url: str = ""):
self.session = requests.Session()
# Enforce a default request timeout on all outgoing requests to prevent
# hung connections from accumulating indefinitely.
http_adapter = _TimeoutHTTPAdapter(timeout=30)
https_adapter = _TimeoutHTTPAdapter(timeout=30)
self.session.mount('http://', http_adapter)
self.session.mount('https://', https_adapter)
self.session.headers.update({
'User-Agent': CONFIG["common"]["ua"]
'User-Agent': CONFIG["common"]["ua"],
# Disable HTTP keep-alive so that TCP connections are closed
# immediately after each response and are not held open in the
# connection pool. Without this, every cached session retains a
# pool of idle sockets; under load this causes the server-wide TCP
# connection count to spike until resources are exhausted.
'Connection': 'close',
})
self.base_url = base_url if base_url else CONFIG["urls"]["base"]["sso_api"]

Expand Down