Python: win32com解决请求或urllib问题

2024-09-29 23:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的解决方案,它可以工作(在公司环境中)

import win32com.client

try:
    import _winreg as winreg
except:
    import winreg

class HTTP:
    proxy = ""
    isProxy = False

    def __init__(self):
        self.get_proxy()

    def get_proxy(self):
        oReg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
        oKey = winreg.OpenKey(oReg, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
        dwValue = winreg.QueryValueEx(oKey, 'ProxyEnable')
        print('[+] dwValue[0] = '+str(dwValue[0]))
        if dwValue[0] == 1:
            oKey = winreg.OpenKey(oReg, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
            dwValue = winreg.QueryValueEx(oKey, 'ProxyServer')[0]
            print('[+] oKey = '+str(oKey))
            print('[+] dwValue = '+str(dwValue))
            self.isProxy = True
            self.proxy = dwValue

    def url_post(self, url, formData):
        httpCOM = win32com.client.Dispatch('Msxml2.ServerXMLHTTP.6.0')

        if self.isProxy:
            print('[+] self.isProxy = true')
            httpCOM.setProxy(2, self.proxy, '<local>')

        httpCOM.setOption(2, 13056)
        httpCOM.Open('POST', url, False)
        httpCOM.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        httpCOM.setRequestHeader('User-Agent', 'whatever you want')

        httpCOM.send(formData)

        return httpCOM.responseText

http = HTTP()

http.url_post('http://bbc.co.uk', 'test=1')

我的问题是如何翻译这里的内容,以便在请求或urllib中使用t

输出为:

[+] dwValue[0] = 1
[+] oKey = <PyHKEY:0x00000000000000000000148>
[+] dwValue = webproxy.f5.pee2peead.local.9090
[+] self.isProxy = true

Tags: importselfhttpurldefwin32comproxyprint

热门问题