2
2
import socket
3
3
import time
4
4
5
- import requests
5
+ import httpx
6
6
import socks
7
7
8
8
from module .conf import settings
13
13
class RequestURL :
14
14
def __init__ (self ):
15
15
self .header = {"user-agent" : "Mozilla/5.0" , "Accept" : "application/xml" }
16
- self ._socks5_proxy = False
17
16
18
17
def get_url (self , url , retry = 3 ):
19
18
try_time = 0
20
19
while True :
21
20
try :
22
- req = self .session .get (url = url , headers = self .header , timeout = 5 )
23
- logger .debug (f"[Network] Successfully connected to { url } . Status: { req .status_code } " )
24
- req .raise_for_status ()
25
- return req
26
- except requests .RequestException :
21
+ req = self .session .get (url = url )
27
22
logger .debug (
28
- f"[Network] Cannot connect to { url } . Wait for 5 seconds. "
23
+ f"[Network] Successfully connected to { url } . Status: { req . status_code } "
29
24
)
25
+ req .raise_for_status ()
26
+ return req
27
+ except httpx .RequestError :
28
+ logger .debug (f"[Network] Cannot connect to { url } . Wait for 5 seconds." )
30
29
try_time += 1
31
30
if try_time >= retry :
32
31
break
33
32
time .sleep (5 )
34
33
except Exception as e :
35
34
logger .debug (e )
36
35
break
37
- logger .error (f"[Network] Unable to connect to { url } , Please check your network settings" )
36
+ logger .error (
37
+ f"[Network] Unable to connect to { url } , Please check your network settings"
38
+ )
38
39
return None
39
40
40
41
def post_url (self , url : str , data : dict , retry = 3 ):
41
42
try_time = 0
42
43
while True :
43
44
try :
44
- req = self .session .post (
45
- url = url , headers = self .header , data = data , timeout = 5
46
- )
45
+ req = self .session .post (url = url , data = data )
47
46
req .raise_for_status ()
48
47
return req
49
- except requests . RequestException :
48
+ except httpx . RequestError :
50
49
logger .warning (
51
50
f"[Network] Cannot connect to { url } . Wait for 5 seconds."
52
51
)
@@ -65,60 +64,42 @@ def check_url(self, url: str):
65
64
if "://" not in url :
66
65
url = f"http://{ url } "
67
66
try :
68
- req = requests .head (url = url , headers = self . header , timeout = 5 )
67
+ req = httpx .head (url = url )
69
68
req .raise_for_status ()
70
69
return True
71
- except requests . RequestException :
70
+ except httpx . RequestError :
72
71
logger .debug (f"[Network] Cannot connect to { url } ." )
73
72
return False
74
73
75
74
def post_form (self , url : str , data : dict , files ):
76
75
try :
77
- req = self .session .post (
78
- url = url , headers = self .header , data = data , files = files , timeout = 5
79
- )
76
+ req = self .session .post (url = url , data = data , files = files )
80
77
req .raise_for_status ()
81
78
return req
82
- except requests . RequestException :
79
+ except httpx . RequestError :
83
80
logger .warning (f"[Network] Cannot connect to { url } ." )
84
81
return None
85
82
86
83
def __enter__ (self ):
87
- self .session = requests .Session ()
88
- if settings .proxy .enable :
89
- if "http" in settings .proxy .type :
90
- if settings .proxy .username :
91
- username = settings .proxy .username
92
- password = settings .proxy .password
93
- url = f"http://{ username } :{ password } @{ settings .proxy .host } :{ settings .proxy .port } "
94
- self .session .proxies = {
95
- "http" : url ,
96
- "https" : url ,
97
- }
98
- else :
99
- url = f"http://{ settings .proxy .host } :{ settings .proxy .port } "
100
- self .session .proxies = {
101
- "http" : url ,
102
- "https" : url ,
103
- }
104
- elif settings .proxy .type == "socks5" :
105
- self ._socks5_proxy = True
106
- socks .set_default_proxy (
107
- socks .SOCKS5 ,
108
- addr = settings .proxy .host ,
109
- port = settings .proxy .port ,
110
- rdns = True ,
111
- username = settings .proxy .username ,
112
- password = settings .proxy .password ,
113
- )
114
- socket .socket = socks .socksocket
115
- else :
116
- logger .error (f"[Network] Unsupported proxy type: { settings .proxy .type } " )
84
+ proxy = self ._build_proxy_url () if settings .proxy .enable else None
85
+ self .session = httpx .Client (
86
+ headers = self .headers , http2 = True , proxies = proxy , timeout = 5
87
+ )
117
88
return self
118
89
119
90
def __exit__ (self , exc_type , exc_val , exc_tb ):
120
- if self ._socks5_proxy :
121
- socks .set_default_proxy ()
122
- socket .socket = socks .socksocket
123
- self ._socks5_proxy = False
124
91
self .session .close ()
92
+
93
+ def _build_proxy_url (self ):
94
+ proxy_type = "http" if "http" in settings .proxy .type else "socks5h"
95
+ auth = (
96
+ f"{ settings .proxy .username } :{ settings .proxy .password } @"
97
+ if settings .proxy .username
98
+ else ""
99
+ )
100
+
101
+ if proxy_type not in ["http" , "socks5h" ]:
102
+ logger .error (f"[Network] Unsupported proxy type: { settings .proxy .type } " )
103
+ return None
104
+
105
+ return f"{ proxy_type } ://{ auth } { settings .proxy .host } :{ settings .proxy .port } "
0 commit comments