views:

372

answers:

2

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.

+1  A: 

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
Anurag Uniyal
Bah, no ctypes in Python 2.4.4 (added in Python 2.5!)
Salim Fadhley
you can install ctypes http://python.net/crew/theller/ctypes/
Anurag Uniyal
Yes, and I got your example to work? I'm still confused about how to do the next bit? I need to make a struct in order to get the return value (http://msdn.microsoft.com/en-us/library/aa383912(VS.85).aspx) no idea how to do that in cTypes.
Salim Fadhley
By the way, I re-asked this question with a 100rep bounty. If you answer here I will award the points. http://stackoverflow.com/questions/1079655/whats-the-correct-way-to-use-win32inet-winhttpgetproxyforurl/1092465#1092465
Salim Fadhley
+1  A: 

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.

RichieHindle
You are awesome! :-)
Salim Fadhley
I can get an Internet handle like this: hinternet = win32inet.InternetOpen("foo 1.0", 0, "", "", 0)But I do not seem to be able to use this handle object to grab the proxy information.
Salim Fadhley
Unfortunately this module seems to be broken - and utterly undocumented. Any idea how to use this?
Salim Fadhley
Sorry, no - I was only going by what the documentation said. You can get ctypes for Python 2.4 from http://sourceforge.net/projects/ctypes/files/ if you want to go down that road. According to Google, someone has done this before and the code is at http://pyapp-project.net/trac/browser/pyapp-framework/trunk/pyapp-library.zip/pyapp/lib/winhttp.py?rev=344 but that site is currently down for me.
RichieHindle