好友
阅读权限20
听众
最后登录1970-1-1
|
# -*- coding: utf-8 -*-
from urllib import parse
from win32com.client import Dispatch
import pythoncom
g_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
def SendWinHttpReq(WinHttpReq, sendUrl, reUrl=False, body=None, reType=1, headDic=None, code='utf-8'):
global g_headers
WinHttpReq.SetOption(6, reUrl)
if body == None:
WinHttpReq.Open('GET', sendUrl, False)
else:
WinHttpReq.Open('POST', sendUrl, False)
WinHttpReq.SetRequestHeader('User-Agent', g_headers['User-Agent'])
if headDic != None:
for i in headDic:
try:
WinHttpReq.SetRequestHeader(i,headDic)
except:
continue
if body == None:
WinHttpReq.Send()
else:
if type(body) != type('11'):
body = parse.urlencode(body)
WinHttpReq.Send(body)
WinHttpReq.WaitForResponse()
if reType==1:
try:
objstr = WinHttpReq.ResponseBody.obj
except:
return ''
if objstr.startswith(b'\xef\xbb\xbf'):
objstr = objstr[3:]
if code!=None:
return objstr.decode(code,'ignore')
return objstr
if reType==2:
try:
return WinHttpReq.GetResponseHeader('Location')
except:
try:
objstr = WinHttpReq.ResponseBody.obj
except:
return ''
if objstr.startswith(b'\xef\xbb\xbf'):
objstr = objstr[3:]
if code != None:
return objstr.decode(code, 'ignore')
return objstr
return WinHttpReq.GetAllResponseHeaders()
def getWinHttpReq(typeid, timeOut=25):
pythoncom.CoInitialize()
WinHttpReq = Dispatch('WinHttp.WinHttpRequest.5.1')
WinHttpReq.SetTimeouts(8*10000, 8*10000, timeOut*10000, timeOut*10000)
if typeid == 2:
HTTPREQUEST_PROXYSETTING_PROXY = 2
WinHttpReq.SetProxy(HTTPREQUEST_PROXYSETTING_PROXY,
"x.x.x.x:xx",
"*.microsoft.com") # 请问这个*.microsoft.com有什么用,我传""也是可以的
return WinHttpReq
if __name__ == '__main__':
winhttp = getWinHttpReq(2)
headData = {
'Content-Type': 'application/x-www-form-urlencoded',
}
postUrl = 'https://www.pubu.com.tw/'
authUrl = SendWinHttpReq(winhttp, postUrl, True, None, 1, headData)
cookie = winhttp.GetAllResponseHeaders()
print(cookie) |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|