python 获取cookie,发送请求

摘要:
获取cookies的思路:1.确认登录界面和登录所需参数(包括用户名、密码、uuid等参数);2.确认uuid和其他参数的采集接口(一般为get请求);3.通过使用uuid和其他参数向登录界面发送请求,以获取响应消息中的cookie(不同的网站平台可能有不同的表示,应区别对待);示例代码:#-*-coding:UTF-8-*-importjson、chardet、requestsimportos

获取cookie思路:

1、确认登录login接口,及登录所需参数(包括用户名、密码、uuid等参数);

2、确认uuid等参数的获取接口(一般是get请求);

3、凭借uuid等参数向login接口发起请求,获取响应报文中的cookie(不同的网站平台可能表示方法不一样,需区别对待);

示例代码:

# -*- coding:UTF-8 -*-
import json,chardet,requests
import os
import httplib
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'

#获取uuid
uuid_url="http://ip:7924/alpha/captchaImage"
uuidresponse=requests.get(uuid_url)#返回response对象
uuidresponse_json=uuidresponse.text #获取响应json报文
uuidresponse_dict=json.loads(uuidresponse_json,encoding="utf-8") #json响应报文转换为字典类型,方便后续数据处理
uuid=uuidresponse_dict['uuid']#从响应中获取uuid
print uuid

#登录并获取cookie
login_url="http://ip:7924/alpha/login?username=XXXX@hq.cmcc&password=admin123&code=1111&uuid="+uuid
login_head={"Content-Type": "application/json; charset=UTF-8", "Connection": "keep-alive"}
login_body={"username":"XXXX@hq.cmcc","password":"admin123","code":"1111","uuid":uuid}

print login_url
print login_body

try:
    res = requests.post(url=login_url, headers=login_head, data=login_body)
    loginresponse_json=res.text
    loginresponse_dict=json.loads(loginresponse_json,encoding="utf-8")
    token = loginresponse_dict['token']
    print 'get token=', token
    cookie ='Bearer '+token
    print 'get cookie=',cookie
    
except Exception as err:
    print '获取cookie失败!',err
    
#保存cookie到文件中
tokenpath=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+'\'
#tokenpath=os.path.abspath(os.path.dirname(os.getcwd()))+'\'
cookiefiledata='NewCookie
'+cookie+'
'
with open(tokenpath+"tianbaoNewCookie.txt",'wb') as f:
    f.write(cookiefiledata)
    
#使用cookie  
filepath = os.path.abspath(os.path.dirname(__file__))+'\'
print 'filepath=',filepath

request_url="http://ip:7924/alpha/light/todo/query/todoTaskList"
request_dictdata={'officeType':'1001','busiType':None,'status':None,'startTime':'','endTime':'','pageNum':1,'pageSize':500,'find':'压测表单业务','officeTypes':['1001'],'busiTypes':[]}

print type(request_dictdata)

requestJSONdata=json.dumps(request_dictdata)

head = {"Content-Type": "application/json; charset=UTF-8", "Connection": "close","Authorization": cookie}

print '>>>>>>requestJSONdata:
',requestJSONdata

try:
    r = requests.post(request_url,data=requestJSONdata,headers=head)
except Exception as e:
    print '【error: 】',e
    raise Exception(e)


responsedata=r.text
print "get the status: ",r.status_code
        

load_dict=json.loads(responsedata,encoding="utf-8")
print '<<<<<<<responsedata:
',load_dict

  

免责声明:文章转载自《python 获取cookie,发送请求》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Latex中为作者添加多个单位属性(IEEE模板)SQLPrompt(可视化工具插件助手) 安装和破解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python 多线程库总结

多线程库总结基于线程的并行性threading模块下面是一些基础函数,函数包括: 函数 threading.active_count() threading.current_thread() threading.get_ident() threading.enumerate() threading.main_thread() threading.se...

vue项目微信端清理缓存问题解决

vue为单页面前端框架,清理缓存,常规的方式是添加html头部meta,如果逻辑里面是需要使用缓存的,这种方式不建议使用,代码片段如下: <html manifest="IGNORE.manifest"> <head> <meta charset="utf-8"> <meta name="vie...

python基础知识5——赋值与深浅拷贝——整数和字符串,列表元组字典

深浅copy      和很多语言一样,Python中也分为简单赋值、浅拷贝、深拷贝这几种“拷贝”方式。 在学习过程中,一开始对浅拷贝理解很模糊。不过经过一系列的实验后,我发现对这三者的概念有了进一步的了解。 一、赋值 赋值算是这三种操作中最常见的了,我们通过一些例子来分析下赋值操作: str例 1 >>> a = 'hello' 2 &...

Python多进程并发操作进程池Pool

目录: multiprocessing模块 Pool类 apply apply_async map close terminate join 进程实例 multiprocessing模块 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择。由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序?由...

robotframework的学习笔记(十三)------Robot Framework常用库简介

标准库Robot Framework可以直接导入使用的库,包括: Builtin:包含经常需要的关键字。自动导入无需import,因此总是可用的 Dialogs:提供了暂停测试执行和从用户的输入方式。 Collections:提供一组关键词处理Python列表和字典。 OperatingSystem:允许执行各种操作系统相关的任务。允许执行各种操作系统相...

python小专题——time模块

time常用函数 最近参与python的一个项目,发现经常遇到一些常用的模块,而每次使用时,我都要查一遍。终于,我决定要各个击破,对常用的python小知识进行总结。下面总结了python中对时间处理的常见函数。 在开始之前,首先要说明这几点: 在Python中,通常有这几种方式来表示时间:1)、时间戳 2)、格式化的时间字符串 3)、元组(struct...