我正在尝试使用urllib2打开URL并将特定的cookie文本发送到服务器.例如,我想打开网站解决国际象棋问题,使用特定的cookie,例如search = 1.我该怎么做?
我正在尝试执行以下操作:
import urllib2
(need to add cookie to the request somehow)
urllib2.urlopen("http://chess-problems.prg")
Run Code Online (Sandbox Code Playgroud)
提前致谢
我试图这样做,
import requests
s=requests.Session()
login_data = dict(userName='user', password='pwd')
ra=s.post('http://example/checklogin.php', data=login_data)
print ra.content
print ra.headers
ans = dict(answer='5')
f=s.cookies
r=s.post('http://example/level1.php',data=ans,cookies=f)
print r.content
Run Code Online (Sandbox Code Playgroud)
但第二个帖子请求返回404错误,有人可以帮助我吗?
我想自动备份需要登录的网站的网页内容。我尝试通过模拟 POST 请求来登录。但我收到错误:
csrf token: CSRF attack detected
Run Code Online (Sandbox Code Playgroud)
以下是我使用的代码的一些摘录:
func postLoginForm(csrfToken string) {
values := make(url.Values)
values.Set("signin[username]", "myusername")
values.Set("signin[password]", "mypassword")
values.Set("signin[_csrf_token]", csrfToken)
resp, err := http.PostForm("https://spwebservicebm.reaktor.no/admin/nb", values)
dumpHTTPResponse(resp) // show response to STDOUT
}
Run Code Online (Sandbox Code Playgroud)
我通过获取登录页面并扫描名为 的隐藏输入字段来获取 csrf 令牌signin[_csrf_token]。执行此操作的代码的重要部分如下:
// Finds input field named signin[_csrf_token] and returns value as csrfToken
func handleNode(n *html.Node) (csrfToken string, found bool) {
if n.Type == html.ElementNode && n.Data == "input" {
m := make(map[string]string)
for _, attr := range n.Attr {
m[attr.Key] = …Run Code Online (Sandbox Code Playgroud)