gpt4 book ai didi

python - 需要帮助理解带有正则表达式和 cURL 的 python 片段

转载 作者:行者123 更新时间:2023-11-28 18:30:58 25 4
gpt4 key购买 nike

编辑 - 刚刚添加了整个 cURL 函数以供引用/更多信息,但需要有关 if 语句的帮助 - 正则表达式

寻求帮助以理解此 cURL 中的 if 语句。我已经阅读了一些 python 文档并且我理解了每一部分,这是用正则表达式搜索和替换。只是希望有人能够帮助给出一个更大的解释。我不太了解 .groups。

为了提供更多背景信息,此脚本通过 cURL 访问另一个站点,它存储一个 cookie,并在运行时检查 cookie 是否有效,如果无效,它会在发布用户名/密码后获取一个新的。该网站最近发生了变化,我正在努力弄清楚我需要更改哪些内容才能使其再次运行。

#get auth cookie for sso
def getAuthCookie( self ):
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.SSL_VERIFYPEER, False)
c.setopt(c.FOLLOWLOCATION, True)
c.setopt(c.TIMEOUT, 60)
c.setopt(c.USERPWD, self.user+":"+cred.getpasswd( self.encPasswd ) )
c.setopt(c.URL, 'https://sso.sample.com')
c.setopt(c.COOKIEJAR, self.cookieDir)
c.setopt(c.COOKIEFILE, self.cookieDir )
c.setopt(c.WRITEFUNCTION, buffer.write)
c.perform()
c.unsetopt(c.USERPWD)
c.setopt(c.URL, 'https://sample.com')
c.perform()
html = str(buffer.getvalue())

----------------------------------------------------------
if "RelayState" in html:
rex = re.compile( "input type=\"hidden\" name=\"RelayState\" value=\"(.*)\"" )
RELAY = rex.search( html ).groups()[0]
if "SAMLResponse" in html:
rex = re.compile( "input type=\"hidden\" name=\"SAMLResponse\" value=\"(.*)\"" )
SAML = rex.search( html ).groups()[0]
datastuff = {'SAMLResponse':SAML,'RelayState':RELAY,'redirect':'Redirect','show_button':'true'}
if "form method=\"POST\" action=" in html:
rex = re.compile( "form method=\"POST\" action=\"(.*)\" " )
postUrl = rex.search( html ).groups()[0]
----------------------------------------------------------

#post our saml obtained, get to our final dest
c.setopt(c.URL, postUrl )
c.setopt(c.POST, True)
c.setopt(c.POSTFIELDS, urlencode( datastuff ))
c.perform()
c.close()

最佳答案

查看我在代码中注入(inject)的注释:

#get auth cookie for sso
def getAuthCookie( self ):
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.SSL_VERIFYPEER, False)
c.setopt(c.FOLLOWLOCATION, True)
c.setopt(c.TIMEOUT, 60)
c.setopt(c.USERPWD, self.user+":"+cred.getpasswd( self.encPasswd ) )
# curling sso.sample.com, which I assume promts a login dialog box and curl will set that with the varible provide above
c.setopt(c.URL, 'https://sso.sample.com')
# save the cookie to cookieDir
c.setopt(c.COOKIEJAR, self.cookieDir)
c.setopt(c.COOKIEFILE, self.cookieDir )
c.setopt(c.WRITEFUNCTION, buffer.write)
# perform all the previous curl commands
c.perform()
c.unsetopt(c.USERPWD)
# curl new site sample.com
c.setopt(c.URL, 'https://sample.com')
c.perform()
# save output as html var
html = str(buffer.getvalue())

----------------------------------------------------------
# The following three if statments
# if "some string is found" in varible-html: then do the lines indented lines that follow
if "RelayState" in html:
# setup a regex to look for "input type="hidden" name="RelayState" value="[and captures everything here this will become the RELAY var]"
rex = re.compile( "input type=\"hidden\" name=\"RelayState\" value=\"(.*)\"" )
# this executes the regex expression on the html var
RELAY = rex.search( html ).groups()[0]
if "SAMLResponse" in html:
rex = re.compile( "input type=\"hidden\" name=\"SAMLResponse\" value=\"(.*)\"" )
# same thing is happening here capturing the value as SAML
SAML = rex.search( html ).groups()[0]
# contructing a new var with strings and the newly contructed vars
datastuff = {'SAMLResponse':SAML,'RelayState':RELAY,'redirect':'Redirect','show_button':'true'}
if "form method=\"POST\" action=" in html:
rex = re.compile( "form method=\"POST\" action=\"(.*)\" " )
# again action="[postURL]"
postUrl = rex.search( html ).groups()[0]
----------------------------------------------------------

#post our saml obtained, get to our final dest
c.setopt(c.URL, postUrl ) # setup curl with url found above
c.setopt(c.POST, True) # use post method
c.setopt(c.POSTFIELDS, urlencode( datastuff )) # post fields found above with newly contructed vars
c.perform()
c.close()

如果有什么改变,你现在得到一个错误,我会在 html = str(buffer.getvalue()) 之后尝试 print html 看看你是否仍然点击期望找到执行正则表达式的同一页面。

关于python - 需要帮助理解带有正则表达式和 cURL 的 python 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com