我在 reddit 的 stackoverflow 上的某个地方找到了这段代码。我尝试为 hackthissite.org 修改它:
import urllib2
import urllib
import cookielib
# Store the cookies and create an opener to hold them
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# Add our headers
opener.addheaders = [('User-agent', 'Tester')]
# Install the opener, changes the global opener to the one we just made
urllib2.install_opener(opener)
# URL for authentification
auth_url = 'https://www.hackthissite.org/user/login'
# Parameters to send
payload = {
'username': 'myUser',
'password': 'myPass',
'btn_submit': 'Login'
}
# Encode payload
data = urllib.urlencode(payload)
# Build request object (supplying 'data' makes it a POST)
req = urllib2.Request(auth_url, data)
# Make request and store in resp
resp = urllib2.urlopen(req)
print resp
据我所知,我得到了正确的有效负载,这是 hackthissite.org 上的登录表单:
<form id="loginform" method="post" action="/user/login">
<div id="innerlogin">
<script type="text/javascript">var userclicked=0; var passclicked=0;</script>
<p><input type="text" name="username" class="login" value="" onclick="if(userclicked==0){this.value='';userclicked=1;};" title="Username" /></p>
<p><input type="password" name="password" class="login" value="" onclick="if(passclicked==0){this.value='';passclicked=1;};" title="Password" /></p>
<p><input type="submit" value="Login" name="btn_submit" class="submit-button" /></p>
</div>
</form>
我从服务器得到的响应是:
<addinfourl at 36515712 whose fp = <socket._fileobject object at 0x022D3DB0>>
如何登录网站?在这种情况下,服务器响应意味着什么?(添加信息网址?)