👨‍💼
Hamm Blog
个人主页GithubGitee
  • 🇨🇳Hi Welcome!
  • 📚分享文章
    • CentOS7
      • CentOS7安装Python3的方法
      • CentOS7搭建L2TP服务端一键脚本
      • CentOS7使用CertBot工具获取LetSencrypt泛域名HTTPS证书
    • Wechat
      • 微信公众号网页跳转避免出现导航栏的小技巧
      • 微信公众号php环境无法获取POST值
    • Git
      • GIT进行代码量统计和贡献统计
      • GIT删除仓库的历史提交记录脚本示例
    • HTTP
      • HTTP关于HEADER的详细解释
      • HTTP关于Code状态码的详细解释
      • HTTP协议对URI长度POST数据长度及COOKIE长度限制说明
    • JavaScript
      • 使用ElementUI配合API数据快速渲染合并表格
      • JavaScript调用语音合成Speech Synthesis API
      • JavaScript解析并遍历Excel示例代码
      • JavaScript下的RSA对称加密DEMO
    • MySQL
      • MySQL腾讯云数据库导出备份到本地服务器导入
      • MySQL设计与使用规范
      • MySQL在5.7及以上版本中的ONLY_FULL_GROUP_BY问题处理方案
      • MySQL字符串截取
    • PHP
      • PHP大文件视频上传WebUploader
      • PHP的坑XML转JSON
      • PHP多进程同时处理任务示例DEMO代码
      • PHP高精度计算常用代码
      • PHP获取上周、本周、上月、本月、本季度、上季度时间方法
      • PHP获取真实IP之IP代理 IP伪造 真实IP
      • PHP获取URL HTTP_HOST和SERVER_NAME
      • PHP简单实现QQ登录代码
      • PHP解密微信小程序手机号简单DEMO
      • PHP实现二叉查找树的简单代码实现
      • PHP实现微博登录简单代码
      • PHP实现Web方式发起一个超时任务的处理请求
      • PHP使用独占文件指针实现阻塞少量并发
      • PHP使用root权限执行系统命令和切换到www用户
      • PHP微信JSSDK开发DEMO
      • PHP下的RSA对称加解密 根证书自签与签发子证书
      • PHP下phpMyAdmin数据字典美化代码
      • PHP下RSA对称加密超长字符串分段加密DEMO
    • Nginx
      • Nginx反向代理中的Host参数传递遇到的坑
      • 利用Nginx实现Java后端在开发中的高可用
      • Nginx反向代理NodeJS实现WSS协议
      • Nginx负载均衡的lnmp环境初始化脚本
      • Nginx配置HTTPS与HTTP2.0
      • Nginx图片递归代理服务器DEMO
    • NodeJS
      • NodeJS控制台简单表格打印
      • NodeJS实现超级方便的Git自动发布Webhook
      • NodeJS实现串口通讯简单例程
      • NodeJS实现微信协议登录
      • NodeJS实现UDP数据报套接字通讯
      • NodeJS实现Websocket聊天室
      • NodeJS使用express模块无法收到phpCurl的值
      • NodeJS使用jimp和qrcode-reader识别解析二维码
      • NodeJS使用Request模块实现CURL
      • NodeJS一些冷门但实用的npm包
      • NodeJS转码amr到mp3
      • NWJS配置文件
    • Python
      • Python3实现Webhook
      • Python3使用jieba分词并生成WordCloud词云图
      • Python3使用Pool进程池实现多进程并发
      • Python3使用request进行CURL操作
      • Python3使用Selenium进行自动化测试手册
    • Linux
      • Linux 信号说明列表说明
      • Linux利用CronTab定时执行ThinkPHP命令行模式
  • 😍分享生活
    • 我有个锤子的生活
由 GitBook 提供支持
在本页
  1. 分享文章
  2. Python

Python3使用request进行CURL操作

def doPost(url,data={},header={},cookie={},timeout=5,proxy={}):
    try:
        _session = requests.session()
        _session.proxies = proxy
        _session.headers.update(header)
        _session.keep_alive = False
        _cookieJar=requests.cookies.RequestsCookieJar()
        for _key,_value in cookie.items():
            _key = str(_key)
            _value = str(_value)
            _cookieJar.set(_key,_value)
        _session.cookies.update(_cookieJar)
        _response = _session.post(url,data=data,proxies=proxy,timeout=timeout)
    except Exception as e:
        return dict({"error":e})
    return dict({"error":None,"body":_response.text,"cookie":_session.cookies.get_dict(),"header":_response.headers})
    
def doGet(url,header={},cookie={},timeout=5,proxy={}):
    try:
        _session = requests.session()
        _session.headers.update(header)
        _session.keep_alive = False
        _cookieJar=requests.cookies.RequestsCookieJar()
        for _key,_value in cookie.items():
            _key = str(_key)
            _value = str(_value)
            _cookieJar.set(_key,_value)
        _session.cookies.update(_cookieJar)
        _response = _session.get(url,proxies=proxy,timeout=timeout)
    except Exception as e:
        return dict({"error":e})
    return dict({"error":None,"body":_response.text,"cookie":_session.cookies.get_dict(),"header":_response.headers})
    
def cookie2string(cookie={}):
    _cookieString = ''
    _index = 0;
    for _key,_value in cookie.items():
        _key = str(_key)
        _value = str(_value)
        if _index == 0:
            _cookieString = _key + "=" + _value
        else:
            _cookieString = _cookieString + ";" + _key + "=" + _value
        _index = _index + 1
    return _cookieString.encode()
    
def post2string(post={}):
    _postString = ''
    _index = 0;
    for _key,_value in post.items():
        _key = str(_key)
        _value = str(_value)
        if _index == 0:
            _postString = _key + "=" + _value
        else:
            _postString = _postString + "&" + _key + "=" + _value
        _index = _index + 1
    return _postString.encode()

上一页Python3使用Pool进程池实现多进程并发下一页Python3使用Selenium进行自动化测试手册
📚