python利用requests模拟http请求及请求头

发布时间:2020-07-24 22:18:11 作者:大宝bj
来源:网络 阅读:61525

一、通过requests发送请求

之前一直使用urllib以及urllib2模拟http请求发送,在实际场景中,我们需要造自己定义好的header、body等等,使用urllib很麻烦,很偶然的机会,接触到了requests,可以通过发送xml、简单易用,直接上代码:

requests 

connected():
    
    #通过形参传入url以及请求参数,get方法发送请求
    (param):
        get = requests.get(==param)
        
        #关闭连接
        get.close()
        
        #返回response
        get.text

    
    #通过形参传入url,测试数据,请求头,post方法发送请求
    (dataheader):
        post = requests.post(=data=header)
        
        #关闭连接
        post.close()
        
        #返回respone
        post.text


二、获得session,并把session插入到header里发送给服务器

from com.utils.ConnUrl import ConnUrl
import requests

class getheader():

    @staticmethod
    def session():
    
    #封装测试数据
        data = 'test data'
        url = ConnUrl.getUrl ()
        url = url+'200000'
    #设置header
        header = {'content-type':'application/octet-stream'}
    #post请求
        post = requests.post(url, data=data, headers=header)
    #通过split函数切割返回数据,获得session
        jsessionid = post.headers['Set-Cookie'].split(';')[0]
        return jsessionid
推荐阅读:
  1. 使用Requests发送Http请求
  2. HTTP协议(7)HTTP请求头

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

post python header

上一篇:开启windows 系统自带FTP服务器

下一篇:数据结构(C语言版)严蔚敏:第三章:栈与队列---栈的应用---p52表达式求值的算法详细分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》