-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathwCurl.py
177 lines (117 loc) · 3.41 KB
/
wCurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#coding=utf-8
'''
wCurl.py
'''
import time
import socket
import httplib
import requests
requests.packages.urllib3.disable_warnings()
from teye_data.config import cfg
from teye_web.http.URL import URL
from teye_web.http.Request import Request
from teye_web.http.Response import Response,from_requests_response
timeout = 60
socket.setdefaulttimeout(timeout)
DEBUGSWITCH = 0 #0关闭调试,1开启调试
class wCurl:
'''
'''
def __init__(self):
'''
'''
self._time = 0.0
self._speed = 20
self._conn = 0
self._hook_manager()
self._scan_signature = cfg["scan_signature"] if cfg.has_key("scan_signature") else "TScanner/1.0"
self._scan_cookies = cfg["scan_cookies"] if cfg.has_key("scan_cookies") else {}
self._scan_proxies = cfg["scan_proxies"] if cfg.has_key("scan_proxies") else {}
httplib.HTTPConnection.debuglevel = DEBUGSWITCH
def _hook_manager(self):
'''
'''
_connect = socket.socket.connect
socket.socket.connect = lambda *args,**kwargs :apply(self._hook_connect,(_connect,self,args,kwargs))
def _hook_connect(self,*args,**kwargs):
'''
'''
realfun,selfobj,realargs,realkwargs = args
while True:
begin = time.time()
now_time = max(0.01,begin-self._time)
if now_time > 5:
self._conn = 0
self._time = now_time
break
if self._conn/now_time<=self._speed:
break
else:
time.sleep(0.01)
self._conn +=1
return apply(realfun,realargs,realkwargs)
def get_default_headers(self,headers):
'''
'''
default_headers = {"User-Agent":self._scan_signature}
default_headers.update(headers)
return default_headers
def get(self,url, headers={}, **kwargs):
'''
'''
default_headers = self.get_default_headers(headers)
if not isinstance(url,URL):
url = URL(url)
requests_response = None
try:
requests_response = requests.get(url.url_string,headers=default_headers,**kwargs)
except:
return self._make_response(requests_response,url)
response = self._make_response(requests_response,url)
return response
def post(self,url, headers={}, data=None, **kwargs):
'''
'''
default_headers = self.get_default_headers(headers)
if not isinstance(url,URL):
url = URL(url)
requests_response = None
try:
requests_response = requests.post(url.url_string,headers=default_headers,data=data,**kwargs)
except:
return self._make_response(requests_response,url)
response = self._make_response(requests_response,url)
return response
def __getattr__(self,name):
'''
'''
print name
#getattr(requests,name.lower())
def _send_req(self,req):
'''
'''
method = req.get_method()
uri = req.get_url().get_uri_string()
querystring = req.get_get_param()
postdata = req.get_post_param()
headers = req.get_headers()
cookies = self._scan_cookies
proxies = self._scan_proxies
send = getattr(requests,method.lower())
requests_response = None
try:
requests_response = send(uri,params=querystring,data=postdata,headers=headers,cookies=cookies,proxies=proxies)
except:
return self._make_response(requests_response,req.get_url())
else:
response = self._make_response(requests_response,req.get_url())
return response
def _make_response(self,resp_from_requests,req_url):
'''
'''
if resp_from_requests is None:
response = Response(req_url=req_url)
else:
response = from_requests_response(resp_from_requests,req_url)
return response
wcurl = wCurl()