我想从Python调用Windows C ++函数WinHttpGetProxyForUrl - 可以这样做吗? [英] I'd like to call the Windows C++ function WinHttpGetProxyForUrl from Python - can this be done?

查看:131
本文介绍了我想从Python调用Windows C ++函数WinHttpGetProxyForUrl - 可以这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft提供了一种方法作为WinHTTP的一部分,它允许用户确定哪个代理应该用于任何给定的URL。它被称为 WinHttpGetProxyForUrl

$不幸的是我在python中编程,所以我不能直接访问这个功能 - 我可以使用Win32COM调用任何Microsoft服务与COM接口。



那么有没有办法从Python访问这个函数?作为一个额外的问题,我不能添加任何除Python之外的项目。这意味着方便的是不可能添加C#或C ++修复。



我在Windows XP上运行带有Win32扩展的Python2.4.4。



更新0:



这是我到目前为止:

  import win32inet 
import pprint
hinternet = win32inet.InternetOpen(foo 1.0,0,,,0)
#不工作!
proxy = win32inet.WinHttpGetProxyForUrl(hinternet,uhttp://www.foo.com,0)


b $ b

很明显,最后一行是错误的,但是我看不到任何正确的方式做任何文档或示例!



更新1:



我要去再次问这是一个新的问题,因为它现在真的关于win32com

解决方案

您可以使用ctypes调用WinHttp.dll中的函数,它是包含'WinHttpGetProxyForUrl的DLL。 '
虽然调用它,你将需要一个HINTERNET会话变量,所以这里我将向你展示第一步,它展示了如何使用ctypes来调用DLL,它产生一个HINTERNET,你必须传递给WinHttpGetProxyForUrl



阅读更多关于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)

打印HINTERNET


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.

解决方案

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

这篇关于我想从Python调用Windows C ++函数WinHttpGetProxyForUrl - 可以这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆