场景如下:
我需要实现一个命令行参数系统,该系统允许
-p <value>
和
-p
(没有给出 -p 的值)
有什么方法可以使用 argparse 来实现此目的,或者我必须手动解析 sys.argv 吗?
非常感谢你!!!
这可能与Python 3.3: Split string and create all Combinations类似的问题密切相关,但我无法从中推断出 pythonic 解决方案。
问题是:
假设有一个诸如 的 str 'hi|guys|whats|app',我需要用分隔符分割该 str 的所有排列。例子:
#splitting only once
['hi','guys|whats|app']
['hi|guys','whats|app']
['hi|guys|whats','app']
#splitting only twice
['hi','guys','whats|app']
['hi','guys|whats','app']
#splitting only three times
...
etc
Run Code Online (Sandbox Code Playgroud)
我可以编写一个回溯算法,但是Python(例如itertools)是否提供了一个简化该算法的库?
提前致谢!!
注意事项:这是我第一个使用 asyncio 的方法,所以我可能做了一些非常愚蠢的事情。
场景如下:
我需要“http-ping”一个庞大的网址列表,以检查它们是否响应 200 或任何其他值。尽管像 gobuster 这样的工具报告 200,403 等,但我对每个请求都会超时。
我的代码与此类似:
import asyncio,aiohttp
import datetime
#-------------------------------------------------------------------------------------
async def get_data_coroutine(session,url,follow_redirects,timeout_seconds,retries):
#print('#DEBUG '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+url)
try:
async with session.get(url,allow_redirects=False,timeout=timeout_seconds) as response:
status = response.status
#res = await response.text()
if( status==404):
pass
elif(300<=status and status<400):
location = str(response).split("Location': \'")[1].split("\'")[0]
print('#HIT '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+str(status)+' '+url+' ---> '+location)
if(follow_redirects==True):
return await get_data_coroutine(session,location,follow_redirects,timeout_seconds,retries)
else:
print('#HIT '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+str(status)+' '+url)
return None
except asyncio.exceptions.TimeoutError as e:
print('#ERROR '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+' '+' '+url+' TIMEOUT '+str(e))
return None …Run Code Online (Sandbox Code Playgroud)