将geopy与经过密码身份验证的代理一起使用 [英] Using geopy with password authenticated proxy

查看:103
本文介绍了将geopy与经过密码身份验证的代理一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将geopy与一组坐标一起使用,起初一切正常.

from geopy.geocoders import GoogleV3
# some coordinates as example
latitude, longitude = 24.47646, 85.3095
geolocator = GoogleV3()
location = geolocator.geocode('%s, %s' (latitude, longitude))
address = location.address
print address
# this would print something like 
Unnamed Road, Burhia Ahri, Jharkand 825406, India

我正在处理的环境需要经过身份验证的代理, 例如,让我们说类似

proxy_address = 'http://proxy.tests.mydomain'
proxy_port = 3128
proxy_user = 'my_user'
proxy_pass = 'my_pass'

假设需要通过身份验证的代理使其正常工作,我查看了geopy github页面文档,即使提到了代理,也找不到一个有效的URL. /p>

似乎支持发送带有'proxies'参数的字典,但是在任何地方都没有提及经过用户/密码验证的变体.

有什么办法可以做到这样吗?

geolocator = GoogleV3(proxies={'http': proxy_address,
                               'password': my_user + my_pass})

拼写

解决方案

对于那些可能遇到相同问题的人,我已经找到了解决涉及到os模块的问题的解决方案.

import os
def set_proxy():
    proxy_addr = 'http://{user}:{passwd}@{address}:{port}'.format(
        user='user_example', passwd='pass_example', 
        address='address_example', port=int('port_example'))
    os.environ['http_proxy'] = proxy_addr
    os.environ['https_proxy'] = proxy_addr

def unset_proxy():
    os.environ.pop('http_proxy')
    os.environ.pop('https_proxy')

set_proxy()
geolocator = GoogleV3()
location = geolocator.geocode('%s, %s' % (latitude, longitude), timeout=10)
address = location.address
unset_proxy()

I'm trying to use geopy with a set of coordinates and everything works fine at first.

from geopy.geocoders import GoogleV3
# some coordinates as example
latitude, longitude = 24.47646, 85.3095
geolocator = GoogleV3()
location = geolocator.geocode('%s, %s' (latitude, longitude))
address = location.address
print address
# this would print something like 
Unnamed Road, Burhia Ahri, Jharkand 825406, India

The enviroment I'm working on requires an authenticated proxy, for example purposes let's say something like

proxy_address = 'http://proxy.tests.mydomain'
proxy_port = 3128
proxy_user = 'my_user'
proxy_pass = 'my_pass'

Asuming the requirement of authenticated proxy to make it work, I've looked at geopy github page and the docs and even if proxies are mentioned, not a single valid url is found.

It seems like sending a dictionary with the 'proxies' parameter is supported, but there is no user/password authenticated variant mentioned anywhere.

Is there any way that something like this is doable?

geolocator = GoogleV3(proxies={'http': proxy_address,
                               'password': my_user + my_pass})

Edit: Spelling

解决方案

For those who may encounter the same problem, I've found a solution to my problem that involves playing with the os module.

import os
def set_proxy():
    proxy_addr = 'http://{user}:{passwd}@{address}:{port}'.format(
        user='user_example', passwd='pass_example', 
        address='address_example', port=int('port_example'))
    os.environ['http_proxy'] = proxy_addr
    os.environ['https_proxy'] = proxy_addr

def unset_proxy():
    os.environ.pop('http_proxy')
    os.environ.pop('https_proxy')

set_proxy()
geolocator = GoogleV3()
location = geolocator.geocode('%s, %s' % (latitude, longitude), timeout=10)
address = location.address
unset_proxy()

这篇关于将geopy与经过密码身份验证的代理一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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