pycharm将代码同步到远程服务器_全局代理「建议收藏」

pycharm将代码同步到远程服务器_全局代理「建议收藏」1.Pycharm设置2.urllib下载数据配置fromurllib.errorimportURLErrorfromurllib.requestimportProxyHandler,build_

1. Pycharm设置

在这里插入图片描述

2. urllib下载数据配置

from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
 
 参数是字典,键名是协议类型,健值是代理
proxy_handler = ProxyHandler({ 
   
    'http': "http://127.0.0.1:12333",
    'https': "http://127.0.0.1:12333"
})
# Opener已经设置好代理了
opener = build_opener(proxy_handler)
try:
    response = opener.open('http://httpbin.org/get')
    # 运行结果是一个JSON
    print(response.read().decode('utf-8'))
except URLError as e:
    print(e.reason)

Torch.geometirc dowload.py数据下载实例:

def download_url(url: str, folder: str, log: bool = True):
    r"""Downloads the content of an URL to a specific folder. Args: url (string): The url. folder (string): The folder. log (bool, optional): If :obj:`False`, will not print anything to the console. (default: :obj:`True`) """

    filename = url.rpartition('/')[2]
    filename = filename if filename[0] == '?' else filename.split('?')[0]
    path = osp.join(folder, filename)

    if osp.exists(path):  # pragma: no cover
        if log:
            print(f'Using existing file {filename}', file=sys.stderr)
        return path

    if log:
        print(f'Downloading {url}', file=sys.stderr)

    makedirs(folder)

    context = ssl._create_unverified_context()
    from urllib.request import ProxyHandler, build_opener
    proxy_handler = ProxyHandler({ 
   
        'http': "http://127.0.0.1:12333",
        'https': "http://127.0.0.1:12333"
    })# Opener已经设置好代理了
    opener = build_opener(proxy_handler)
    data = opener.open(url)
    # data = urllib.request.urlopen(url, context=context)

    with open(path, 'wb') as f:
        f.write(data.read())

    return path

今天的文章pycharm将代码同步到远程服务器_全局代理「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/78969.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注