企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# requests [python2.7模块文档][3],这是一个第三方模块 ## get 发送一个get请求 `requests.get(url, params=None, **kwargs)` kwargs包括 ~~~ headers={} 头部信息 verify=true 是否验证https ~~~ ### 不带参数 ~~~ #!/usr/bin/python import requests url="http://my.os/notification/charm/" r = requests.get(url) print r.status_code print r.content ~~~ ### 带参数 ~~~ #!/usr/bin/python import requests url="http://my.os/notification/charm/" payload={'message': "Opportunities and challenges together"} r = requests.get(url, params=payload) print r.status_code print r.content ~~~ ### 定制请求头 ~~~ #!/usr/bin/python import requests url="http://my.os/notification/charm/" headers={'Authorization': 'token 52ee7d4c57686ca8d6884fa4c482a28'} payload={'message': "Opportunities and challenges together"} r = requests.get(url, headers=headers, params=payload) print r.status_code print r.content ~~~ ## post 发送一个post请求 `requests.post(url, data=None, json=None, **kwargs)` kwargs包括 ~~~ headers={} 头部信息 verify=true 是否验证https ~~~ >[info] get和post的方法返回对象有以下方法: > * url 请求的url > * status_code 状态码 > * text 返回的内容 >* content 返回的内容 ### session 返回一个session对象,用于处理session相关 `requests.session()` ### HTTPError 用于捕捉报错 `requests.HTTPError` #### 登录企业邮箱 这里需要使用token作为身份凭证,因此需要先获取token,在post数据时使用相同的token。 ~~~ #!/usr/bin/python #encoding:utf8 import urllib2 import requests import re class MyGS(object): def __init__(self): self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0', "Referer":"https://bjmail.clustertech.com/roundcube/"} self.baseURL = 'https://bjmail.clustertech.com/roundcube/' self.loginURL = self.baseURL + '?_task=login' def getPage(self): # 生成session对象 self.session = requests.session() try: response = self.session.get(url=self.baseURL, headers=self.headers,verify=False) return response.text except urllib2.URLError, e: if hasattr(e, 'reason'): print "连接服务器失败, 错误原因", e.reason return None def getToken(self): page = self.getPage() re_token = re.compile(r'<form.*?<input.*?value="(.*?)".*?>', re.S) token = re_token.search(page) if token: print token.group(1) return token.group(1) else: return None def login(self): data = {"_token":self.getToken(), "_task":"login", "_action":"login", "_timezone":"Asia/Shanghai", "_url":"", "_user":"zyli", "_pass":"12345.com"} try: response = self.session.post(url=self.loginURL, data = data,headers = self.headers, verify = False) print response.text except urllib2.URLError, e: print e gs = MyGS() gs.getToken() gs.login() ~~~ #### 登录126邮箱 ~~~ #!/usr/bin/python #encoding:utf8 import urllib2 import requests import re #headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'} headers = {'User_Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0'} params = { "language":"-1", "style":"-1", "df":"mail126_letter", "from":"web", "allssl":"false", "race":"-2_-2_-2_db", "net":"failed", "iframe":"1", "product":"mail126", "passtype": '0', "savelogin": '1', "url2":"http://mail.126.com/errorpage/error126.htm", "funcid": "loginone", } data = { "username": "cvyangyanan@126.com", "password": "e79179c47aaae49ae63c8456bb0ca087",} url = "https://mail.126.com/entry/cgi/ntesdoor?" session = requests.session() login = session.post(url=url, data=data, headers=headers, params=params) #print login.url #print login.status_code #print login.text re_url = re.compile(r'href = "(.*?)"') url = re_url.findall(login.text)[0] print url ~~~ [3]:https://pypi.python.org/pypi/request/0.0.12