完成一個requests + session + post + get的類

這個類還沒有完成,不過己經(jīng)實(shí)現(xiàn)session的保存,http認(rèn)證,發(fā)送數(shù)據(jù),接收數(shù)據(jù)的功能

代碼如下

# -*- coding: utf8 -*-

import http;

import urllib3

import urllib

import urllib.request

from Action.SYS.Log import *

from Action.SYS.ShowProcess import *

from io import BytesIO

import sys

import re

import time

import requests.sessions

import http.cookiejar

from requests.auth import HTTPBasicAuth

import requests

from Action.File.FileEvent import *

from Action.SYS.get import *

class HTTP2():

? ? __httpPool=None

? ? __timeout = 30;

? ? __httpCode=0

? ? __httpConnectionCode=0

? ? __curl=None

? ? __file=None

? ? __get=None

? ? __debug=False

? ? __httpdownloadfilesize=0

? ? __lostConnectStart=0

? ? __httpdownloadcontinue=''

? ? __lostConnectSkipTime=10

? ? __lostConnectCounter=3

? ? __status=0

? ? __body=''

? ? __uploadfiles={}

? ? __proxy={}

? ? __auth={}

? ? __cookieFile=''

? ? __cookie=''

? ? __request=None

? ? data="";

? ? def __init__(self,timeout,debug=False):

? ? ? ? self.__timeout=timeout;

? ? ? ? self.__file=FileEvent()

? ? ? ? self.__get=get()

? ? ? ? self.__debug=debug

? ? ? ? self.__request=requests.session()

? ? def __writeCookie(self,cookie=""):

? ? ? ? try:

? ? ? ? ? ? self.__file.writeFile(self.__cookieFile,cookie)

? ? ? ? except Exception as e:

? ? ? ? ? ? msg = "write data[%s] to cookie[%s] error(%s)" % (str(cookie),self.__cookieFile, str(e))

? ? ? ? ? ? Log.debug(self.__debug, msg)

? ? ? ? ? ? Log.log('error', msg)

? ? def setProxy(self,proxys={"http":"httpurl","https":"https url"}):

? ? ? ? self.__proxy=proxys

? ? ? ? return self

? ? def auth(self,username,password):

? ? ? ? self.__auth=HTTPBasicAuth(username,password)

? ? ? ? return self

? ? def setCookiePath(self,path="/home/***/tmp/cookie.txt"):

? ? ? ? try:

? ? ? ? ? ? self.__cookieFile=path

? ? ? ? ? ? self.__file.writeFile(self.__cookieFile,"","w")

? ? ? ? except Exception as e:

? ? ? ? ? ? self.__cookieFile="cookie.txt"

? ? ? ? ? ? msg = "create cookie[%s] error(%s)" % (self.__cookieFile, str(e))

? ? ? ? ? ? Log.debug(self.__debug, msg)

? ? ? ? ? ? Log.log('error', msg)

? ? ? ? return self

? ? def query(self, url, method="POST|GET", data="{'name':'value'}", headers={},cookies={}):

? ? ? ? r=None

? ? ? ? if method=="" or method=="GET":

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? r=self.__request.get(url,headers=headers,cookies=cookies,proxies=self.__proxy,auth=self.__auth)

? ? ? ? ? ? ? ? self.__status=r.status_code

? ? ? ? ? ? ? ? self.__body=str(r.content,encoding='utf-8')

? ? ? ? ? ? except Exception as e:

? ? ? ? ? ? ? ? msg="request.get(%s) error(%s)" %(url,str(e))

? ? ? ? ? ? ? ? Log.debug(self.__debug,msg)

? ? ? ? ? ? ? ? Log.log('error',msg)

? ? ? ? else:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? r=self.__request.post(url,data,files=self.__uploadfiles,headers=headers,cookies=cookies,proxies=self.__proxy,auth=self.__auth)

? ? ? ? ? ? ? ? self.__status = r.status_code

? ? ? ? ? ? ? ? self.__body = str(r.content, encoding='utf-8')

? ? ? ? ? ? except Exception as e:

? ? ? ? ? ? ? ? msg="request.post(%s) data[%s] error(%s)" %(url,str(data),str(e))

? ? ? ? ? ? ? ? Log.debug(self.__debug,msg)

? ? ? ? ? ? ? ? Log.log('error',msg)

? ? ? ? #print(self.__request.cookies.get_dict())

? ? ? ? return self

? ? def get_status(self):

? ? ? ? return self.__status

? ? def get_body(self):

? ? ? ? return self.__body


未經(jīng)本人同意

代碼不得任意轉(zhuǎn)發(fā)

更不得用于商業(yè)用途

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容