I'd like to call the Windows C++ function WinH

2019-06-04 15:49发布

Microsoft provides a method as part of WinHTTP which allows a user to determine which Proxy ought to be used for any given URL. It's called WinHttpGetProxyForUrl.

Unfortunately I'm programming in python so I cannot directly access this function - I can use Win32COM to call any Microsoft service with a COM interface.

So is there any way to get access to this function from Python? As an additional problem I'm not able to add anything other than Python to the project. That means however convenient it is impossible to add C# or C++ fixes.

I'm running Python2.4.4 with Win32 extensions on Windows XP.

Update 0:

This is what I have so far:

import win32inet
import pprint
hinternet = win32inet.InternetOpen("foo 1.0", 0, "", "", 0)
# Does not work!!!
proxy = win32inet.WinHttpGetProxyForUrl( hinternet, u"http://www.foo.com", 0  )

Obviously the last line is wrong, however I cannot see any docs or examples on the right way to do it!

Update 1:

I'm going to re-ask this as a new question since it's now really about win32com.

2条回答
孤傲高冷的网名
2楼-- · 2019-06-04 16:13

This page at ActiveState: WINHTTP_AUTOPROXY_OPTIONS Object implies that WinHttpGetProxyForUrl is available in the win32inet module of the Win32 extensions. SourceForge is currently broken so I can't download it to verify whether it is or not.

Edit after "Update 0" in the question:

You need to pass a WINHTTP_AUTOPROXY_OPTIONS and a WINHTTP_PROXY_INFO as documented here on MSDN: WinHttpGetProxyForUrl Function.

查看更多
孤傲高冷的网名
3楼-- · 2019-06-04 16:27

You can use ctypes to call function in WinHttp.dll, it is the DLL which contains 'WinHttpGetProxyForUrl. ' Though to call it you will need a HINTERNET session variable, so here I am showing you the first step, it shows how you can use ctypes to call into DLL,it produces a HINTERNET which you have to pass to WinHttpGetProxyForUrl, that I will leave for you as exercise, if you feel difficulty POST the code I will try to fix it.

Read more about ctypes @ http://docs.python.org/library/ctypes.html

import ctypes

winHttp = ctypes.windll.LoadLibrary("Winhttp.dll")

WINHTTP_ACCESS_TYPE_DEFAULT_PROXY=0
WINHTTP_NO_PROXY_NAME=WINHTTP_NO_PROXY_BYPASS=0
WINHTTP_FLAG_ASYNC=0x10000000
# http://msdn.microsoft.com/en-us/library/aa384098(VS.85).aspx
HINTERNET = winHttp.WinHttpOpen("PyWin32", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC)

print HINTERNET
查看更多
登录 后发表回答