这个类还没有完成,不过己经实现session的保存,http认证,发送数据,接收数据的功能
代码如下
# -*- 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
未经本人同意
代码不得任意转发
更不得用于商业用途