简体   繁体   中英

Python and SSL — EOF occurred in violation of protocol

I'm trying to login to the web-site via Python. I have the following code:

import sys
sys.path.append('ClientCookie-1.3.0')
import ClientCookie
sys.path.append('ClientForm-0.2.10')
import ClientForm

cookieJar = ClientCookie.CookieJar()

opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cookieJar))
opener.addheaders = [("User-agent","Mozilla/5.0 (compatible)")]
ClientCookie.install_opener(opener)
fp = ClientCookie.urlopen("login_page_url")
forms = ClientForm.ParseResponse(fp)
fp.close()

# print forms on this page
for form in forms: 
    print("***************************")
    print(form)

form = forms[2]
form["username"] = "some_username"
form["password"] = "some_password"
fp = ClientCookie.urlopen(form.click())
fp.close()
fp = ClientCookie.urlopen("some_url_for_authorized_users_only")
html = fp.read()
fp.close();
print(html.decode('utf-8'))

Output

URLError: <urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol>

What am I doing wrong? What does it mean? How can I fix this error?

Looks like the problem you have is caused by the obsolete version of OpenSSL. Check the version by

 $ openssl version -a

and

 $ python -c "import ssl; print ssl.OPENSSL_VERSION"

These may not be consistent. The first command shows the version your system use. If you see a different (possible older) version for second command, which means python doesn't refer to the latest openssl you installed.

In my case, first result was 0.9~ so I upgraded OpenSSL via homebrew, to 1.0.2k. Then I reinstalled python to enforce the newer version.

howtoupgradessl is a good reference to upgrade ssl if you use mac. In that case, you also might want to refer this as well since newer osx have stronger security which you need to unlock.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM