wg4568 wg4568 - 1 year ago 188
Python Question

Python - NameError: name 'PROTOCOL_SSLv3' is not defined, when using gevent

So I had a Flask app on one machine in a virtualenv (raspberry pi running raspbian) and everything worked just fine, however when I ported it to another raspberry pi, also running raspbian, and set up a new virtualenv to the exact same specifications, running the app throws this:

The Flask app is a socket-io chat app, that uses ajax and stuff.

Traceback (most recent call last):
File "application.py", line 10, in <module>
from flask.ext.socketio import SocketIO, emit
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/flask/exthook.py", line 62, in load_module
__import__(realname)
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 2, in <module>
monkey.patch_all()
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/gevent/monkey.py", line 185, in patch_all
patch_socket(dns=dns, aggressive=aggressive)
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/gevent/monkey.py", line 124, in patch_socket
from gevent import socket
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/gevent/socket.py", line 659, in <module>
from gevent.ssl import sslwrap_simple as ssl, SSLError as sslerror, SSLSocket as SSLType
File "/home/host/chat-server/venv/local/lib/python2.7/site-packages/gevent/ssl.py", line 386, in <module>
def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None):
NameError: name 'PROTOCOL_SSLv3' is not defined
Exception KeyError: KeyError(3061183472L,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored


The process of setting up the venv was as follows,

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt


requirements.txt looks like,

Flask==0.10.1
Flask-SocketIO==0.3.7
Jinja2==2.7.2
MarkupSafe==0.23
Werkzeug==0.9.4
gevent==1.0.1
gevent-socketio==0.3.6
gevent-websocket==0.9.3
greenlet==0.4.2
ipython==2.1.0
itsdangerous==0.24
pyreadline==2.0


and when running
venv/bin/pip freeze
, both machines give the exact same packages and versions.

BTW, my application.py is as follows, although I don't think it's part of the problem, since it works fine on my first machine (yes, I know it's sloppy, but it was an experiment).

from flask.ext.socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context, request, session, redirect
from random import random
from time import sleep
import json, pickle, os, binascii, pickler

app = Flask(__name__)
app.config['SECRET_KEY'] = binascii.b2a_hex(os.urandom(16))
app.config['DEBUG'] = True
app.config['PICKLE_RESET'] = "KGxwMAou"
app.config['BAD_NAMES'] = ["wg4568"]
app.config['SECRET_PASSWORD'] = "thepassword"

#turn the flask app into a socketio app
socketio = SocketIO(app)

class Reciever():
def __init__(self):
self.messages = pickler.load("messages")

def send(self, user, message):
if len(message):
self.messages.insert(0, (user, message))
pickler.save(self.messages, "messages")
socketio.emit('newmsg', {'content': message, 'user': user}, namespace='/msg')
return "Sent from " + user + " that reads, " + message
else:
return "Message was blank, not sent"

def render(self):
# if not session["VIEW"]:
# return '<p id="alert"><strong>' + self.messages[0][0] + ': </strong>' + self.messages[0][1] + '</p>'
# else:
if 1:
html = ""
for msg in self.messages[:session["VIEW"]]:
if msg[0] == "ALERT":
html += '<p id="alert"><strong>' + msg[0] + ': </strong>' + msg[1] + '</p>'
else:
html += '<p><strong>' + msg[0] + ': </strong>' + msg[1] + '</p>'
return html

rec = Reciever()

@app.before_request
def before_request():
try: session["VIEW"]
except KeyError: session["VIEW"] = 0
try: session["USERNAME"]
except KeyError: session["USERNAME"] = "AnonUser-" + binascii.b2a_hex(os.urandom(4))
# if not request.url.split("/")[-1:][0] == "send":
# rec.send("ALERT", session["USERNAME"] + " has joined the room")

@app.route('/user_newid')
def user_newid():
session["USERNAME"] = "AnonUser-" + binascii.b2a_hex(os.urandom(4))
return redirect("/")

@app.route('/user_setid', methods=["POST"])
def user_setname():
username = request.form["username"]
canbypass = False
if username.split("-")[-1:][0] == app.config["SECRET_PASSWORD"]:
canbypass = True
username = username.split("-")[0]
if not username in app.config['BAD_NAMES'] or canbypass:
session["USERNAME"] = username
return redirect("/")

@app.route('/send', methods=["POST"])
def send():
user = request.form["user"]
content = request.form["content"]
return rec.send(user, content)

@app.route('/', methods=["GET", "POST"])
def index():
if request.args.get("viewall"): session["VIEW"] += 10
else: session["VIEW"] = 0
print session["VIEW"]
return render_template('index.html', old=rec.render(), username=session["USERNAME"])

@socketio.on('connect', namespace='/msg')
def test_connect():
print('Client connected')

@socketio.on('disconnect', namespace='/msg')
def test_disconnect():
# rec.send("ALERT", session["USERNAME"] + " has left the room",)
print('Client disconnected')


if __name__ == '__main__':
socketio.run(app, host="0.0.0.0")


Does anyone have any clue what is going on here, I am completely stumped.

Answer Source

I solved this problem by replacing PROTOCOL_SSLv3 = PROTOCOL_SSLv23 , thank to answer of mgogoulos , link here: https://github.com/mistio/mist.io/issues/434

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download