Connecting to Internet?

江枫思渺然 提交于 2019-12-08 13:57:46

问题


I'm having issues with connecting to the Internet using python.

I am on a corporate network that uses a PAC file to set proxies. Now this would be fine if I could find and parse the PAC to get what I need but I cannot.

The oddity:

R can connect to the internet to download files through wininet and .External(C_download,...) so I know it is possible and when I do:

import ctypes

wininet = ctypes.windll.wininet
flags = ctypes.wintypes.DWORD()
connected = wininet.InternetGetConnectedState(ctypes.byref(flags), None)
print(connected, hex(flags.value))

I get: 1 0x12 so I have a connection available but once I try to use other functions from within wininet I'm constantly met with error functions like:

AttributeError: function 'InternetCheckConnection' not found

and this goes for pretty much any other function of wininet, but this doesn't surprise me as the only named function in dir(wininet) is InternetGetConnectedState.

The wininet approach can clearly work, but I have no idea how to proceed with it [especially given that I only use Windows in work].


回答1:


"ok, so poor wording - let's just change that to: open a connection to a web page and obtain its content using python "

Sounds like you actually need BeautifulSoup and Requests. Here's a quick example of them being used to explore a webpage




回答2:


First, I would strongly suggest to install the requests module. Doing HTTP without it on Python is pretty painful.

According to this answer you need to download wpad.dat from the host wpad. That is a text file that contains the proxy address.

Once you know the proxy settings, you can configure requests to use them:

import requests

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}

requests.get('http://example.org', proxies=proxies)


来源:https://stackoverflow.com/questions/41597294/connecting-to-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!