PySocks proxy service and urllib urlopen request 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)

 

  Configured proxy address: If the browser uses proxy service, it should be consistent with the browser configuration server

 

        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 uses the service proxy function to download pip install PySocks 
official website information https://pypi.org/project/PySocks/

Note: Pay special attention to the encoding format of the article f '{len (chunk): X} \ r \ n'.encode (' ascii ') + chunk \ TypeError: can't concat str to bytes

Guess you like

Origin www.cnblogs.com/zzxp/p/12699042.html