PySocks 代理服务和urllib urlopen 请求https



 1   header={}
 2         header['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'
 3         header['Accept']='application/json, text/javascript, */*; q=0.01'
 4         header['Accept-Encoding'] ='gzip, deflate, br'
 5         header['Accept-Language']='zh-CN,zh;q=0.9'
 6         header['Cookie']='JSESSIONID=A6CF81D8FEC72100394EBFF5F6187F52'
 7         header['X-Requested-With']='XMLHttpRequest'
 8         header['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8'
 9         data={'path':'/disk'}
10         params = urllib.parse.urlencode(data).encode(encoding='UTF8')
11         socket = socks.socksocket()
12         socket.set_proxy(socks.PROXY_TYPE_SOCKS5,'127.0.0.1',8888)

 

  配置的代理地址:    若浏览器采用代理服务, 则跟要和浏览器配置服务器一致

 

        try:
            re=urllib.request.Request(url=url,data=params,headers=header)
            re_data = urllib.request.urlopen(re)
            re_data = re_data.read()
            code = json.loads(re_data)
            print(code)
        except URLError as e:
            print(e.strerror)

 

PySocks使用服务代理功能  下载 pip install PySocks
官网资料  https://pypi.org/project/PySocks/

注意 :  特别注意文章的编码格式问题  f'{len(chunk):X}\r\n'.encode('ascii') + chunk \       TypeError: can't concat str to bytes

上一篇:基本库使用(urllib,requests)


下一篇:在Python的urllib2 urlopen中检测超时错误