Maximum recursion depth exceeded on SSLContext (eventlet, flask, flask-socketio, tweepy) - python

I'm building a flask app that displays real-time data visualizations of tweets based on a user input. Right now, the client is still hosted locally and I'm just trying to get to the point where a json of a single tweet gets emitted from the server to the client. This is also my first time building a flask app on my own, so I've been learning HTML/CSS/node.js/JS along the way (be gentle with me).
My problem is with the backend/server. Here is the relevant flask code:
from flask import Flask, request, render_template, session
from flask_socketio import SocketIO, emit
from time import sleep
import threading
from threading import Thread, Event, Lock
import eventlet
import json
import TweePoll_API_nostream
eventlet.monkey_patch(socket=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socket = SocketIO(app, async_mode=None, logger=True, engineio_logger=True)
streamer2 = TweePoll_API_nostream.getTweet()
thread = None
thread_lock = Lock()
def background_thread():
count = 0
while True:
socket.sleep(10)
x = streamer2.main(v)
count += 1
socket.emit('my_response',
{'data': x, 'count': count},
namespace='/output')
#app.route('/', methods = ['GET','POST'])
def index():
return(render_template("index.html", async_mode = socket.async_mode))
#socket.on('my_event', namespace='/output')
def test_message(message):
global v
v = message['data']
print(v)
global thread
with thread_lock:
if thread is None:
thread = socket.start_background_task(background_thread)
emit('my_response', {'data': v, 'count': 0})
if __name__ == '__main__':
socket.run(app, port = 5000, debug = True)
The error occurs on x = streamer2.main(v) in the background_thread() function. Here is the relevant code for the tweepy streamer:
import tweepy
import config
import TweePoll_NLP_single
import json
MLObj = TweePoll_NLP_single.ReadyForSQL()
class getTweet(object):
def __init__(self):
self.auth = tweepy.auth.OAuthHandler(config.ckey, config.csecret)
self.auth.set_access_token(config.atoken, config.asecret)
self.api = tweepy.API(self.auth)
def main(self, keywrd):
tweet = str()
user = str()
time = str()
status = self.api.search(q = [keywrd + " #realDonaldTrump", keywrd + " #JoeBiden"], count = 1, result_type = "recent", lang = 'en')[0]
if hasattr(status, "retweeted_status"): # Check if Retweet
try:
tweet = status.retweeted_status.extended_tweet["full_text"]
except AttributeError:
tweet = status.retweeted_status.text
else:
try:
tweet = status.extended_tweet["full_text"]
except AttributeError:
tweet = status.text
user = status.author.screen_name
time = str(status.created_at)
prez = MLObj.whichPrez(tweet)
clean = MLObj.kleenex(tweet)
NRCemo = MLObj.NRCEmo(clean)
printDF = json.dumps({"tweet":tweet, "clean":clean,
"prez":prez, "NRCEmo":NRCemo, "time":time, "username":user})
return(printDF)
This code isn't reproducible, because it connects to another file for the NLP/sentiment tagging, plus the frontend isn't included. Let me know if I need to send a link to my github or something to fix this.
Here is the error output:
Exception in thread Thread-6:
Traceback (most recent call last):
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py", line 183, in execute
resp = self.session.request(self.method,
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn
conn.connect()
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 342, in connect
self.ssl_context = create_urllib3_context(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 276, in create_urllib3_context
context.options |= options
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
[Previous line repeated 481 more times]
RecursionError: maximum recursion depth exceeded
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\swagj\Documents\GitHub\TweePoll\TweePoll_Server_V2.py", line 28, in background_thread
x = streamer2.main(v)
File "C:\Users\swagj\Documents\GitHub\TweePoll\TweePoll_API_nostream.py", line 21, in main
status = self.api.search(q = [keywrd + " #realDonaldTrump", keywrd + " #JoeBiden"], count = 1, result_type = "recent", lang = 'en')[0]
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py", line 250, in _call
return method.execute()
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py", line 191, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py", line 183, in execute
resp = self.session.request(self.method,
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn
conn.connect()
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 342, in connect
self.ssl_context = create_urllib3_context(
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 276, in create_urllib3_context
context.options |= options
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "C:\Users\swagj\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 602, in options
super(SSLContext, SSLContext).options.__set__(self, value)
[Previous line repeated 481 more times]
tweepy.error.TweepError: Failed to send request: maximum recursion depth exceeded
Final dump, here is my info:
Windows 10
python = 3.8.3
eventlet = 0.25.2
tweepy = 3.8.0
websocket-client = 0.57.0
websockets = 8.1
Flask = 1.1.2
Flask-SocketIO = 4.3.0
I have no idea why this doesn't work, but I have a feeling it has to do with eventlet. Any clues? Happy to give more info if needed :)

For anyone else stumbling upon this... I was getting this error on a Flask app using eventlet (running Python 3.9):
File "/usr/lib/python3.9/ssl.py", line 603, in options
super(SSLContext, SSLContext).options.set(self, value)
File "/usr/lib/python3.9/ssl.py", line 603, in options
super(SSLContext, SSLContext).options.set(self, value)
File "/usr/lib/python3.9/ssl.py", line 603, in options
super(SSLContext, SSLContext).options.set(self, value)
[Previous line repeated 490 more times]
RecursionError: maximum recursion depth exceeded
To make a long story short:
disabling eventlet.monkey_patch() corrected the issue in tests, but I wanted to keep it.
updating eventlet and dnspython fixed it for good for me.
Before:
dnspython==1.16.0
eventlet==0.30.2
After:
dnspython==2.3.0
eventlet==0.33.3

Related

AWS Lambda connection times out due socket errors

My AWS lambda connection keeps timing out due to socket errors. I have set out the timeout for the lambda to be 12 minutes in the code (EDIT: I mean in the lambda console) and the lambda is not in private vpc so the problem is not related to that.
For example: I run a function that runs 450 seconds. The function runs successfully in the AWS but it is above some magical threshold so the python code waits until the timeout and then throws an error. If the lambda runtime is less than the magical threshold then the python code works without any errors.
Might it be some kind of socket error that then loses the connection after some time but keeps waiting until the socket times out? I have no experience related to how sockets work in Ubuntu machines.
My code for testing:
Lambda code:
import json
from time import sleep
def lambda_handler(event, context):
print("SLEEPING: " + str(event["sleep_time"]) + "s")
sleep(event["sleep_time"])
print("SLEEP ENDED")
return {
"statusCode": 200,
"sleep_time": event["sleep_time"]
}
Code to invoke lambda:
import boto3
from botocore.client import Config
import json
from datetime import datetime
read_timeout = 500
connect_timeout = 500
sleep_time = 450
config = Config(
read_timeout=read_timeout,
connect_timeout=connect_timeout,
retries={"max_attempts": 0}
)
lambda_client = boto3.client(
"lambda",
aws_access_key_id="xxx",
aws_secret_access_key="xxx",
config=config
)
st = datetime.now()
print(f"STARTED AT: {st}")
print(f"\tSLEEP TIME PARAMETER: {sleep_time}")
print(f"\tCONFIG read_timeout {read_timeout}")
print(f"\tCONFIG connect_timeout {connect_timeout}")
try:
lambda_client.invoke(
FunctionName="helloWorld",
Payload=json.dumps({"sleep_time": sleep_time})
)
finally:
et = datetime.now()
print(f"ENDED AT: {et}")
print(f"SECONDS TAKEN TO COMPLETE: {et - st}")
The function works when the sleep_time is set to low e.q. 3 but I keep running into errors when I set the sleep_time to larger values.
Output with sleep_time=3
STARTED AT: 2022-08-02 18:17:04.015493
SLEEP TIME PARAMETER: 3
CONFIG read_timeout 500
CONFIG connect_timeout 500
ENDED AT: 2022-08-02 18:17:07.357435
SECONDS TAKEN TO COMPLETE: 0:00:03.341942
Output with longer sleeptime:
STARTED AT: 2022-08-02 19:03:36.196755
SLEEP TIME PARAMETER: 450
CONFIG read_timeout 500
CONFIG connect_timeout 500
ENDED AT: 2022-08-02 19:11:56.553257
SECONDS TAKEN TO COMPLETE: 0:08:20.356502
And I get the following error message:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.8/http/client.py", line 1348, in getresponse
response.begin()
File "/usr/lib/python3.8/http/client.py", line 316, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.8/http/client.py", line 277, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/.local/lib/python3.8/site-packages/botocore/httpsession.py", line 448, in send
urllib_response = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 376, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 703, in reraise
raise value
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 423, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 330, in _raise_timeout
raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: AWSHTTPSConnectionPool(host='lambda.eu-west-1.amazonaws.com', port=443): Read timed out. (read timeout=900)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/lambda_test.py", line 24, in <module>
lambda_client.invoke(
File "/home/user/.local/lib/python3.8/site-packages/botocore/client.py", line 508, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/user/.local/lib/python3.8/site-packages/botocore/client.py", line 898, in _make_api_call
http, parsed_response = self._make_request(
File "/home/user/.local/lib/python3.8/site-packages/botocore/client.py", line 921, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "/home/user/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 119, in make_request
return self._send_request(request_dict, operation_model)
File "/home/user/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 202, in _send_request
while self._needs_retry(
File "/home/user/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 354, in _needs_retry
responses = self._event_emitter.emit(
File "/home/user/.local/lib/python3.8/site-packages/botocore/hooks.py", line 412, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/home/user/.local/lib/python3.8/site-packages/botocore/hooks.py", line 256, in emit
return self._emit(event_name, kwargs)
File "/home/user/.local/lib/python3.8/site-packages/botocore/hooks.py", line 239, in _emit
response = handler(**kwargs)
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 207, in __call__
if self._checker(**checker_kwargs):
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 284, in __call__
should_retry = self._should_retry(
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 320, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 363, in __call__
checker_response = checker(
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 247, in __call__
return self._check_caught_exception(
File "/home/user/.local/lib/python3.8/site-packages/botocore/retryhandler.py", line 416, in _check_caught_exception
raise caught_exception
File "/home/user/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 281, in _do_get_response
http_response = self._send(request)
File "/home/user/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 377, in _send
return self.http_session.send(request)
File "/home/user/.local/lib/python3.8/site-packages/botocore/httpsession.py", line 485, in send
raise ReadTimeoutError(endpoint_url=request.url, error=e)
botocore.exceptions.ReadTimeoutError: Read timeout on endpoint URL: "https://lambda.eu-west-1.amazonaws.com/2015-03-31/functions/helloWorld/invocations"
Edit:
Cloudwatch log for the errorcase:
2022-08-02T19:03:36.526+03:00 START RequestId: feffa401-62a2-4c77-8a92-96be27bdceeb Version: $LATEST
2022-08-02T19:03:36.529+03:00 SLEEPING: 450s
2022-08-02T19:11:06.638+03:00 SLEEP ENDED
2022-08-02T19:11:06.640+03:00 END RequestId: feffa401-62a2-4c77-8a92-96be27bdceeb
2022-08-02T19:11:06.640+03:00 REPORT RequestId: feffa401-62a2-4c77-8a92-96be27bdceeb Duration: 450102.24 ms Billed Duration: 450103 ms Memory Size: 128 MB Max Memory Used: 37 MB
The one that succeeded is similar to this

Azure Instance with linux machine docker timing out on requests

I have a python script in a docker that runs and collects data from an api on a loop.
`for idx, items in enumerate(SearchList):
#get access token
access_token = GetPricingAccTok()
#request id to get results
reqid = RequestIdCompSearch(items, access_token)
#api to get results by doing a while loop appending results together till api returns 100% as completed percentage
Result = GetReqResults(reqid, access_token)
Pricings = GetPricingDict(Result)
filenamepricing = "pricinghistory/CorrectFormat/Pricings_Data_{idn}_{ts}.json".format(
idn = idx,
ts= dt.datetime.now().isoformat()
)
GetAzContainer().upload_blob(
name=filenamepricing,
data=json.dumps(obj= Pricings, indent=4),
blob_type='BlockBlob'
)
`
When i run and test this code locally on my windows machine it works well without an issue and completes the requests
but in the docker on azure container instance it then returns an error showing that the connection has timed out. Not sure what the difference is between the running it locally and in the docker that i then recieve this error.
I have tried addinng this code at the start
import socket
from urllib3.connection import HTTPConnection
HTTPConnection.default_socket_options =(HTTPConnection.default_socket_options + [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) #Enables the feature
,(socket.SOL_TCP, socket.TCP_KEEPIDLE, 45) #Overrides the time when the stack willl start sending KeppAlives after no data received on a Persistent Connection
,(socket.SOL_TCP, socket.TCP_KEEPINTVL, 10) #Defines how often thoe KA will be sent between them
,(socket.SOL_TCP, socket.TCP_KEEPCNT, 60) #How many attemps will your code try if the server goes down before droping the connection.
]
)
but it still times out. showing the below error
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/usr/local/lib/python3.8/http/client.py", line 1348, in getresponse
response.begin()
File "/usr/local/lib/python3.8/http/client.py", line 316, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python3.8/http/client.py", line 277, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/local/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/usr/local/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/usr/local/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 440, in send
resp = conn.urlopen(
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/local/lib/python3.8/site-packages/urllib3/util/retry.py", line 532, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/local/lib/python3.8/site-packages/urllib3/packages/six.py", line 770, in reraise
raise value
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 447, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 353, in _raise_timeout
raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='service.xxx.com', port=443): Read timed out. (read timeout=None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "src/script.py", line 298, in <module>
reqid = RequestIdCompSearch(items, access_token)
File "src/script.py", line 111, in RequestIdCompSearch
InitResp =rq.post( ReqUrl, headers= ReqHead, data=Jsondata)
File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 117, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 532, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='service.xxxx.com', port=443): Read timed out. (read timeout=None)

Takes 1 positional argument but 2 error while python requests module

I have the following test code
import requests
import json
r = requests.get('https://api.github.com/user')
print(r.status_code)
This is getting the following error, I reinstalled requests and urllib3 modules but didn't help
File "/data//temp/scratchprojects/data structures/test.py", line 4, in <module>
r = requests.get('https://api.github.com/user')
File "/data/my_venv/lib/python3.8/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/data/my_venv/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/data/my_venv/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/data/my_venv/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/data/my_venv/lib/python3.8/site-packages/requests/adapters.py", line 412, in send
conn = self.get_connection(request.url, proxies)
File "/data/my_venv/lib/python3.8/site-packages/requests/adapters.py", line 315, in get_connection
conn = self.poolmanager.connection_from_url(url)
File "/data/my_venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 298, in connection_from_url
return self.connection_from_host(
File "/data/my_venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 245, in connection_from_host
return self.connection_from_context(request_context)
File "/data/my_venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 260, in connection_from_context
return self.connection_from_pool_key(pool_key, request_context=request_context)
File "/data/my_venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 281, in connection_from_pool_key
pool = self._new_pool(scheme, host, port, request_context=request_context)
File "/data/my_venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 213, in _new_pool
return pool_cls(host, port, **request_context)
File "/data/my_venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 906, in __init__
HTTPConnectionPool.__init__(
File "/data/my_venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 197, in __init__
self.pool = self.QueueCls(maxsize)
TypeError: __init__() takes 1 positional argument but 2 were given
looks like a corrupted library. After removing the virtual environment and recreating it fixed the issue

python Flask SocketIO throwing SSL error with eventlet

I am running this python code where I am trying to run a socketio flask application and passing ssl certificate files:
from flask import Flask, render_template, request, session, Markup, current_app, jsonify
from flask_socketio import emit, SocketIO
import eventlet
from flask_babel import gettext
app = Flask(__name__)
app.config['SECRET_KEY'] = '123'
app.config['FILEDIR'] = 'static/_files/'
socketio = SocketIO(app)
if __name__ == '__main__':
try:
app_host = os.environ.get('APP_HOST')
app_port = os.environ.get('APP_PORT')
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen((app_host, int(app_port))),certfile ='selfsigned.crt', keyfile = 'selfsigned.key',server_side = True),app)
except Exception as e:
logger.error(e)
When I run this code it throws following SSL error:
(21755) wsgi starting up on https://12.34.56.78:5000
(21755) accepted ('12.34.56.79', 50021)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 458, in fire_timers
timer()
File "/usr/local/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 58, in __call__
cb(*args, **kw)
File "/usr/local/lib/python3.6/site-packages/eventlet/greenthread.py", line 218, in main
result = function(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 781, in process_request
proto.__init__(conn_state, self)
File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 335, in __init__
self.handle()
File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 368, in handle
self.handle_one_request()
File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 397, in handle_one_request
self.raw_requestline = self._read_request_line()
File "/usr/local/lib/python3.6/site-packages/eventlet/wsgi.py", line 380, in _read_request_line
return self.rfile.readline(self.server.url_length_limit)
File "/usr/local/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 204, in recv_into
return self._base_recv(nbytes, flags, into=True, buffer_=buffer)
File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 225, in _base_recv
read = self.read(nbytes, buffer_)
File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 139, in read
super(GreenSSLSocket, self).read, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/eventlet/green/ssl.py", line 113, in _call_trampolining
return func(*a, **kw)
File "/usr/local/lib/python3.6/ssl.py", line 871, in read
return self._sslobj.read(len, buffer)
File "/usr/local/lib/python3.6/ssl.py", line 631, in read
v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: HTTP_REQUEST] http request (_ssl.c:2217)
What I want is that this app should run over https connection but this error is preventing it from running. Below are my python and package version details:
python 3.6.3
eventlet==0.22.1
Flask==0.12.2
Flask-SocketIO==2.9.3
It seems that the request that has been made to the server is using http instead of https. You have to make sure that the client is also making a request using the same https protocol as the server. For example in you client code, you have to make a request to https://<server-ip> instead of http://<server-ip>

how to increase timeout in tornado

I'm trying tornado framework. But I found tornado frequently failed after 30 seconds in the stress test (using multi-mechanize). I use 10 threads in multi-mechanize and run for 100 seconds, around 500 requests / seconds. And it's around 15% failure ratio after 30 seconds. The whole test is about 100 seconds. From the statistics, I realize, the failure may due to timeout after 0.2 seconds. I searched for several ways to increase the timeout on web, but nothing works.
The below is my tornado code:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
#tornado.web.asynchronous
def get(self):
self.write("Hello, world")
self.finish()
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8000)
tornado.ioloop.IOLoop.instance().start()
Here is my multi-mechanize test script:
import requests
import random
import time
class Transaction(object):
def __init__(self):
pass
def run(self):
r = requests.get('http://127.0.0.1:8000')
output = r.raw.read()
assert(r.status_code == 200)
return output
if __name__ == '__main__':
trans = Transaction()
trans.run()
print trans.custom_timers
The following is the error message I got from multimech-run
Traceback (most recent call last):
File "././test_scripts/v_user.py", line 12, in run
r = requests.get('http://127.0.0.1:8000')
File "/Library/Python/2.7/site-packages/requests/api.py", line 54, in get
return request('get', url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/safe_mode.py", line 37, in wrapped
return function(method, url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 42, in request
return s.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 230, in request
r.send(prefetch=prefetch)
File "/Library/Python/2.7/site-packages/requests/models.py", line 603, in send
timeout=self.timeout,
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 415, in urlopen
body=body, headers=headers)
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 267, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output
self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send
self.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 740, in connect
self.timeout, self.source_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 49] Can't assign requested address
To answer your question of how to change the timeout for tornado, you need to modify tornado's bind_socket function to set the time out: http://www.tornadoweb.org/documentation/_modules/tornado/netutil.html
sock.setblocking(0)
sock.bind(sockaddr)
sock.listen(backlog)
sockets.append(sock)
Change the first line to sock.setblocking(1). According to the documentation: http://docs.python.org/library/socket.html#socket.socket.settimeout :
Setting a timeout of None disables timeouts on socket operations
s.settimeout(None) is equivalent to s.setblocking(1).
However, as suggested in the comment, I think you should look at distributing the load.

Categories