如何告诉 R 在 Windows 中使用代理自动配置脚本 (PAC) [英] How to tell R to use proxy auto config script (PAC) in Windows

查看:61
本文介绍了如何告诉 R 在 Windows 中使用代理自动配置脚本 (PAC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用连接到 Internet 上另一个 URL 的 R 函数(例如 read_htmlurl.exists 等)和获取页面超时等我可以从浏览器连接.我相信这是因为 R 没有使用我办公室网络中规定的代理设置.

I am unable to use R functions that connects to another URL on the Internet (e.g. read_html, url.exists, etc) and getting time-outs etc for pages that I can connect from a browser. I believe this is because R is not using the proxy setting mandated in my office network.

我看过另一个关于为 R 设置代理的问题 但我的情况有所不同,因为我们为代理使用了自动配置脚本.

I've looked at another question on setting proxy for R but my situation differs in that we use an auto-configuration script for the proxy.

我已经尝试设置以下

setInternet2(F)
Sys.setenv(http_proxy_user="userid:password")
Sys.setenv(http_proxy="http://myproxypac.mydomain/proxy.pac")

但是没有用.

有人对在 R 中处理 PAC 有什么建议吗?

Anyone has suggestions on handling PACs in R?

推荐答案

在 R 中有多个 Internet 客户端可用,因此这取决于您使用的是什么.

There are several internet clients available in R so it depends on what you are using.

pac 文件不是代理服务器.它只是客户端需要执行的一段 JavaScript 来计算给定 URL 所需的代理服务器.所以你上面的代码肯定是错误的.

A pac file is not a proxy server. It is just a piece of JavaScript that the client needs to execute to calculate the required proxy server for a given URL. So your code above is definitely wrong.

当需要不同的代理服务器来连接不同的主机(例如特殊的内网代理)时,公司会使用 pac.如果您的 pac 文件,请查看源代码以了解发生了什么.curl 包在 ie_get_proxy_for_url() 函数中实现了一个实际的 PAC 客户端.因此,您可以将其包装起来以自动查找并设置卷曲句柄的正确代理(另请参见 博客):

Companies use pac when different proxy servers are required to connect different hosts (e.g. a special intranet proxy). Have a look at the source code if your pac file to see what's going on. The curl package implements an actual PAC client in the ie_get_proxy_for_url() function. So you could wrap that to automatically find and set the correct proxy for a curl handle (see also blog):

curl_with_proxy <- function(url, verbose = TRUE){
  proxy <- ie_get_proxy_for_url(url)
  h <- new_handle(verbose = verbose, proxy = proxy)
  curl(url, handle = h)
}

然后像这样使用它:

con <- curl_with_proxy("https://httpbin.org/get")
readLines(con)

如果您的 pac 文件只返回 proxy.<my.domain>:8080 您可以在环境变量中设置的任何 URL,但这仅适用于基于 libcurl 的客户端:

If it turns out your pac file simply returns proxy.<my.domain>:8080 for any URL you might be able to set in an environment variable, but this only works for libcurl based clients:

Sys.setenv(http_proxy_user = "userid:password")
Sys.setenv(http_proxy = "proxy.<my.domain>:8080")

如果你不能让它工作,请在这个github问题.也许您的案例可以帮助我们改进 curl 包的这一部分.

If you can't get it to work, please describe your problem in this github issue. Perhaps your case can help us improve this part of the curl package.

这篇关于如何告诉 R 在 Windows 中使用代理自动配置脚本 (PAC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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