使用打开器时如何使用urllib获取状态代码

时间:2018-11-14 20:15:33

标签: python python-3.x urllib

我正在使用以下代码将socks4代理与urllib一起使用:

import socks
import socket
import urllib.request, urllib.parse, urllib.error
import http.cookiejar
import http.client as httplib
proxy = task
        ip, port = proxy.split(":")
        socket.setdefaulttimeout(timeout_value)
        log_msg = str("Trying Socks4 proxy%21s " % proxy)
        socks.set_default_proxy(socks.SOCKS4, ip, int(port))
        socket.socket = socks.socksocket
        cj = http.cookiejar.CookieJar()
        opener = urllib.request.build_opener(
                    urllib.request.HTTPCookieProcessor(cj),
                    urllib.request.HTTPRedirectHandler(),
        )
        try:
                opener.addheaders = [('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30')]
                response = opener.open("https://www.google.com/search?q=iamtesting", timeout=timeout_value).read().decode("utf-8")

现在,如何获取状态代码,如404 200 502,我尝试了response.code()和response.getcode(),但是都说它们不存在。

0 个答案:

没有答案