Skip to content

Instantly share code, notes, and snippets.

@kaka19ace
Created March 26, 2014 16:33
Show Gist options
  • Save kaka19ace/9787480 to your computer and use it in GitHub Desktop.
Save kaka19ace/9787480 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#coding=utf-8
import cookielib
import urllib
import urllib2
import json
import base64
from urlparse import urlparse
AUTHORIZATION = "Authorization"
def get(url):
# nginx 验证
username = 'kaka'
password = '123456'
base64str = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64str
# web login 验证
data = {"username":"user01", "password":"passwd"}
data = urllib.urlencode(data)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
# login
# post 请求
req = urllib2.Request("http://admin.kaka.com/login", data)
# 首先通过 nginx auth basic, 修改http header 实现
req.add_header(AUTHORIZATION, authheader)
resp = opener.open(req)
#return resp.read()
# 登录后访问其他链接, 如果有nginx 做的 auth限制,每次请求需要在 header上修改
req = urllib2.Request(url)
req.add_header(AUTHORIZATION, authheader)
resp = opener.open(req)
return resp.read()
def main():
url = "http://admin.kaka.com/url1"
print get(url)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment