import requests import re def script(url): proxy={ 'http':'127.0.0.1', 'https':'127.0.0.1' } try: data=['Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'] http=requests.post(url=url,data=data,proxies=proxy) if re.search('Error-Based Fields',http): print('存在sql注入,路径遍历漏洞') elif re.search('Debugging Information',http): print('存在敏感信息泄露或代码执行漏洞') elif re.search('Injection Echo',http): print('存在sql注入,或命令注入漏洞') else: print('没有找到漏洞') except Exception as f: print(f'错误{f}') script('https://www.zhihu.com/')帮我优化代码
时间: 2024-04-22 13:28:15 浏览: 17
你的代码已经相当简洁和有效了。不过,我有几个建议可以帮助你进一步优化代码:
1. 使用 `requests` 库的 `proxies` 参数代替手动设置代理字典,这样可以更简洁地指定代理。例如:
```python
proxies = {
'http': 'http://127.0.0.1',
'https': 'http://127.0.0.1',
}
```
2. 使用 `requests` 库的 `headers` 参数来发送请求头,而不是将 User-Agent 直接放在请求数据中。这样可以更准确地模拟浏览器发送的请求。例如:
```python
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0',
}
http = requests.post(url=url, headers=headers, proxies=proxies)
```
3. 建议将漏洞类型和相应的处理逻辑封装成函数,以提高代码的可读性和可维护性。例如:
```python
def check_vulnerabilities(http):
if re.search('Error-Based Fields', http):
return '存在 SQL 注入、路径遍历漏洞'
elif re.search('Debugging Information', http):
return '存在敏感信息泄露或代码执行漏洞'
elif re.search('Injection Echo', http):
return '存在 SQL 注入、命令注入漏洞'
else:
return '没有找到漏洞'
def script(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0',
}
http = requests.post(url=url, headers=headers, proxies=proxies)
vulnerabilities = check_vulnerabilities(http)
print(vulnerabilities)
except Exception as e:
print(f'错误:{e}')
script('https://www.zhihu.com/')
```
这样,你的代码会更加易读和易于维护。希望对你有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)