Python模拟登录

首先编写一个非常小的网站应用,该网站能实现注册,登录,拦截器(也就是用户在没有登录的情况,不能访问某些网页),可使用该例子:tornado_blog_practice,该网站的http://localhost:/temp页面在用户没有登录的情况下,是不能够访问的。

模拟登录

首先运行该网站应用:python main.py

# -*- coding: utf-8 -*-
import requests

# 登录
def getTemp(username, password):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'}
    session = requests.session()
#    session.get("http://192.168.126.138", headers=headers)
    postdata = {
        "username": username,
        "password": password
    }
    url = "http://192.168.126.138/login"
    login = session.post(url, data=postdata, headers=headers)
    web_page = session.get("http://192.168.126.138/temp")
    print web_page.content    

# 没有登录的情况
def beforeGet():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'}
    session = requests.session()
#    session.get("http://192.168.126.138", headers=headers)
    web_page = session.get("http://192.168.126.138/temp", headers=headers)
    print web_page.content  

if __name__ == "__main__":
    beforeGet()
    print "---------------------"
    username = "liyuling"
    password = "li"
    getTemp(username, password)
lee@bogon:tornado-test$ python spider.py 
<!doctype html>
<html lang="en">
<head>

<title>Login Page</title>

</head>
<body>

<h2>If you want to login, please input your name and password.<h2>
<form action="/login", method="post">
username: <input type="text", name="username">
password: <input type="password", name="password">
<input type="submit" value="Submit">
</form>

</body>
</html>

---------------------
<!doctype html>
<html lang="en">
<head>

<title>temp</title>

</head>
<body>

<h1>Temp page.</h1>

</body>
</html>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部