正在连接互联网? [英] Connecting to Internet?

查看:144
本文介绍了正在连接互联网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用python连接到Internet时遇到问题。

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

我在一个使用PAC文件设置代理的公司网络中。现在,如果我能找到并解析PAC来获取所需的东西,但我却不能,那就很好了。

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.

奇怪的地方:

R可以通过wininet和.External(C_download,...)连接到互联网以下载文件,因此我知道这是可能的,当我这样做时:

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))

我得到:1 0x12,所以我有一个可用的连接,但是一旦我尝试从wininet中使用其他功能,我就经常遇到错误功能,例如:

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

这几乎与wininet的其他任何功能都用了,但这并不奇怪,因为dir(wininet)中唯一的命名函数是Internet

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.

wininet方法显然可以工作,但我不知道如何进行此工作(特别是考虑到我仅在工作中使用Windows)。

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].

推荐答案

首先,我会强烈建议安装请求模块。

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

根据此答案您需要从主机 wpad 下载 wpad.dat 。那是一个包含代理地址的文本文件。

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)

这篇关于正在连接互联网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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