python封装的GET&POST请求

python封装的GET&POST请求,第1张

概述python封装的GET&POST请求

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

    #Coding=utf-8      import os      import urllib      import httplib      import requests      import Json            def get_page_content(baseUrl,path,params,headers,type = 'get'):          try:              httpClIEnt = httplib.httpconnection(baseUrl,80,timeout=30)              if type == 'get':                  httpClIEnt.request('GET',path)                  response = httpClIEnt.getresponse()                  print response.status                  print response.reason                  print response.read()              elif type == 'post':                  params = urllib.urlencode(params)                  httpClIEnt.request("POST",headers)                  response = httpClIEnt.getresponse()                  print response.status                  print response.reason                  print response.read()                  print response.getheaders() #获取头信息              else:                  print 'unsupport'          except Exception,e:              print Exception,e                          def get_page_content_by_requests(url,payload,timeout,type = 'get'):            try:              if type == 'get':                  response = requests.get(url,params=payload,timeout=timeout)                  response.enCoding = 'gbk' #设定编码                  print response.status_code  #返回状态                  print response.text #返回字符结果                  print response.content #返回二进制响应内容                  print response.Json() #获取Json响应内容                  print response.raw #获取原始响应内容                  print response.raise_for_status() #非200请况下抛出异常                  print response.headers['Content-Type'] #获取响应头某个字段              elif type == 'post':                  response = requests.post(url,data = Json.dumps(payload),headers = headers,timeout=timeout)                  response.enCoding = 'gbk' #设定编码                  print response.text              else:                  print 'unsupport'          except Exception,e  

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python封装的GET&POST请求全部内容,希望文章能够帮你解决python封装的GET&POST请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1198816.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存