python - Requests.get 没有完成,不会引发任何错误

标签 python macos tkinter python-requests

我在运行 High Sierra 的 osx 上遇到了问题。

问题是在下面的代码中,对 requests.get 的调用没有做任何事情。它根本不会退出调用,也不会引发错误。 get 调用后的 print 方法没有被执行,但应用程序没有崩溃,它只是像什么都没发生一样继续到主循环。

同样的代码在 Linux 上运行良好。我正在研究如何在 Windows 上尝试它,并希望再安装一个 osx。

我在这里完全不知所措。它甚至不引发错误这一事实对我来说很奇怪。

import tkinter as tk
from tkinter import ttk
import multiprocessing
import time
import requests


class SplashScreen(tk.Toplevel):

    def __init__(self, root):
        super(SplashScreen, self).__init__(root)

        self.width = 800
        self.height = 400
        self.geometry('{Width}x{Height}'.format(Width=self.width, Height=self.height))
        self.overrideredirect(True)

        self.label = ttk.Label(self, text='My Splash', anchor='center')
        self.label.grid(column=0, row=2)

    def destroy_splash_screen(self):
        self.destroy()
        print('destroyed splash')


class App(tk.Tk):

    def __init__(self):
        super(App, self).__init__()

        self.splash = SplashScreen(self)

        self.withdraw()

        process_startup = multiprocessing.Process(
            target=self.startup_process,
            args=("stuff",)
        )
        process_startup.start()
        self.splash.update()

        while process_startup.is_alive():
            time.sleep(0.1)

        self.title("MyApp")

        self.mainloop()

    def mainloop(self, n=0):
        first_loop = True
        while True:
            self.update_idletasks()
            self.update()
            if first_loop:
                self.remove_splash_screen()
                first_loop = False

    def startup_process(self, things):
        init_client()

    def remove_splash_screen(self):
        self.splash.destroy_splash_screen()
        del self.splash
        self.deiconify()

def init_client():
    requests.get("http://httpbin.org/ip")
    s = 'strfff'
    print(s)


if __name__ == '__main__':
    app = App()

此外,如果我使用调试器跟踪它,它会在请求库的这一点进入黑洞。

def should_bypass_proxies(url, no_proxy):
    """
    Returns whether we should bypass proxies or not.

    :rtype: bool
    """
    # Prioritize lowercase environment variables over uppercase
    # to keep a consistent behaviour with other http projects (curl, wget).
    get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())

    # First check whether no_proxy is defined. If it is, check that the URL
    # we're getting isn't in the no_proxy list.
    no_proxy_arg = no_proxy
    if no_proxy is None:
        no_proxy = get_proxy('no_proxy')
    parsed = urlparse(url)

    if parsed.hostname is None:
        # URLs don't always have hostnames, e.g. file:/// urls.
        return True

    if no_proxy:
        # We need to check whether we match here. We need to see if we match
        # the end of the hostname, both with and without the port.
        no_proxy = (
            host for host in no_proxy.replace(' ', '').split(',') if host
        )

        if is_ipv4_address(parsed.hostname):
            for proxy_ip in no_proxy:
                if is_valid_cidr(proxy_ip):
                    if address_in_network(parsed.hostname, proxy_ip):
                        return True
                elif parsed.hostname == proxy_ip:
                    # If no_proxy ip was defined in plain IP notation instead of cidr notation &
                    # matches the IP of the index
                    return True
        else:
            host_with_port = parsed.hostname
            if parsed.port:
                host_with_port += ':{}'.format(parsed.port)

            for host in no_proxy:
                if parsed.hostname.endswith(host) or host_with_port.endswith(host):
                    # The URL does match something in no_proxy, so we don't want
                    # to apply the proxies on this URL.
                    return True

    with set_environ('no_proxy', no_proxy_arg):
        # parsed.hostname can be `None` in cases such as a file URI.
        try:
    # It executes this method and returns when stepped through but from here it just ends. doesn't go anywhere from this point
            bypass = proxy_bypass(parsed.hostname)
        except (TypeError, socket.gaierror):
            bypass = False

    if bypass:
        return True

    return False

此代码在 requests/utils.py 中

关于从哪里开始的任何想法。就像我说的,我现在完全不知所措。

最佳答案

MacOS 上的 requests 似乎有点问题。尝试通过设置 trust_env 来禁用代理为 False:

session = requests.Session()
session.trust_env = False  # No proxy settings from the OS
r = session.get(url)

关于python - Requests.get 没有完成,不会引发任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55408047/

相关文章:

python - 从可变数量的按钮 PyQt 获取颜色

python - Python 如何自省(introspection)激活了哪些语言功能?

objective-c - NSOutputStream 给出 -1 字节?

macos - 避免 Mac 应用程序在未处理的异常后崩溃

python - 如何使用小部件选项值作为条件来制作 if 语句?

python - 使用来自同一数据帧的 x 和 y 绘制直方图

python - 给定一个 python 2.7.2 类型,我是否可以以编程方式构造一个类实例?

cocoa - 设置 Cocoa 应用程序中按钮文本的格式

python - 返回 tkinter 列表框中选定值的列表

python - 填充行时更改Excel文件中的行