How to handle 307 redirection using urllib2 from http to https

こ雲淡風輕ζ 提交于 2021-01-28 05:19:34

问题


try:
    client_result = urllib2.urlopen( "http://" + data['src_ip'] + "/iperf/iperf_main.py.fcgi", urllib.urlencode(data), 3 )
    client_response_text = client_result.read()
    return('200 OK', response_headers, ['success', server_response_text, client_response_text])
except urllib2.HTTPError as e:
    return(str(e.code) + ' Error' , response_headers, [e.read()])

The code snippet above makes a HTTP POST request and gets a 307 redirect to the same address, except HTTPS version. However, the code control flow goes to the HTTPError exception. I thought the default HTTPRedirectHandler would automatically take care of this redirection. I don't care about the security implication of what I am doing (everything is encrypted in a lower layer anyway).

How can I redirect to the HTTPS page without handling it in the HTTPError code block? Thanks!

来源:https://stackoverflow.com/questions/44662147/how-to-handle-307-redirection-using-urllib2-from-http-to-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!