1

有人可以帮我完成 python 工作并登录到这些值吗?以下

  <div class='field'>
  <label for='login'>Login</label><br/>
  <input id='login' name='login' size='16' type='text' />
</div>
<div class='field'>
  <label for='login_password'>Password</label><br/>
  <input id='login' name='password' size='16' type='password' />
</div>
<div class='field'>
  <input type='submit' value='Login' />
</div>

我厌倦了用这段代码尝试数百万次

import sys,os,time,subprocess,threading,readline,fileinput, urllib, urllib2, cookielib
usernameInsnort = ''
passwordInsnort = ''
print 'can you pleasse insert your snort user name:'
usernameStr = raw_input(usernameInsnort)
print 'can you please insert your snort password:'
passwordStr = raw_input(passwordInsnort)
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
login_data = urllib.urlencode({'login' : usernameStr, 'login' : passwordStr})
opener.open('https://www.snort.org/login', login_data)
resp = opener.open ('https://www.snort.org/downloads/2142')
print resp.read()

谢谢

4

1 回答 1

0

您错过了源代码中的一些 HTML 引用,这是快速说明您的方法不起作用的原因所必需的

<form action="https://www.snort.org/session" 
    method="post">
    <input name="authenticity_token" 
        type="hidden" value="y4xmS1IJatlRmkFz4GT4OrBuE57cBCtHj0ka0UPg9sw=" />
    <fieldset> 
        [Snip - You already provided this bit]
    </fieldset>
</form>

从这个 HTML 我们可以确定两个问题

  • 表格张贴/session/login
  • 有一个authenticity_token字段可以防止 Cross Site Request Forgery (CSRF)似乎不是问题,可能是因为还没有设置用户会话

从您提供的原始 Paython 代码中,请注意这一行

login_data = urllib.urlencode({'login' : usernameStr, 'login' : passwordStr})

其中确定了另一个问题

  • 您永远不会对密码的值进行编码,因为它被编码为登录

因此,考虑到所有这些,我们可以通过进行一些更改来使您的代码正常工作

import sys,os,time,subprocess,threading,readline,fileinput, urllib, urllib2, cookielib
usernameInsnort = ''
passwordInsnort = ''
print 'can you pleasse insert your snort user name:'
usernameStr = raw_input(usernameInsnort)
print 'can you please insert your snort password:'
passwordStr = raw_input(passwordInsnort)
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 

# Changed the second dictionary item to password instead of login
login_data = urllib.urlencode({'login' : usernameStr, 'password' : passwordStr})

# Changed the URL opened to that the login form submits to
opener.open('https://www.snort.org/session', login_data)

resp = opener.open ('https://www.snort.org/downloads/2142')
print resp.read()
于 2013-01-20T23:36:44.627 回答