bottle.py WSGI server stops responding - wsgi

I'm trying to build a simple API with the bottle.py (Bottle v0.11.4) web framework. To 'daemonize' the app on my server (Ubuntu 10.04.4), I'm running the shell
nohup python test.py &
, where test.py is the following python script:
import sys
import bottle
from bottle import route, run, request, response, abort, hook
#hook('after_request')
def enable_cors():
response.headers['Access-Control-Allow-Origin'] = '*'
#route('/')
def ping():
return 'Up and running!'
if __name__ == '__main__':
run(host=<my_ip>, port=3000)
I'm running into the following issue:
This works initially but the server stops responding after some time (~24hours). Unfortunately, the logs don't contain any revealing error messages.
The only way I have been able to reproduce the issue is when I try to run a second script on my Ubuntu server that creates another server listening to a different port (ie.: exactly the same script as above but port=3001). If I send a request to the newly created server, I also do not get a response and the connection eventually times out.
Any suggestions are greatly appreciated. I'm new to this, so if there's something fundamentally wrong with this approach, any links to reference guides would also be appreciated. Thank you!

Can you make sure the server isn't sleeping.
If it is, try enabling Wake On LAN http://ubuntuforums.org/showthread.php?t=234588

Related

Telethon doesn't send messages when running as service

I've created a script which sends messages using telethon. The receivers are not always the same: the number of receivers and their IDs are taken from a MySQL table. The multi processing script runs okay in the expected loop when started from the command prompt. But as soon as it's started as a service the messages are not send.
Please see the code below which includes the function to send out the messages. This function is called by another function which loops over the result of a MySQL query.
Can someone shine a light on the question why the function runs fine from the prompt and not as a service?
import configparser
# get configuration
config = configparser.ConfigParser()
config.read('/etc/p2000.cfg')
telegram_api_id = config.get('telegram','api_id')
telegram_api_hash = config.get('telegram','api_hash')
telegram_bot_name = config.get('telegram','bot_name')
client = TelegramClient(telegram_bot_name, telegram_api_id, telegram_api_hash)
def p2k_send_telegram(PeerID,Message):
async def main():
await client.send_message(int(PeerID), Message)
with client:
client.loop.run_until_complete(main())
Okay, the answer was easy and right in front of me! The issue could be isolated to the client variable. When running as a service under systemd the session (file) has to be defined with its full path!
Something like this:
client = TelegramClient('/full/path/to/my.session', telegram_api_id, telegram_api_hash)

Lambda function gets stuck when calling RDS via SQLalchemy URI

I have a fast API application. Initially, I was passing my DB URI via ngrok tunnel like this in my SAM template. In this setup Lambda will be using my local machine's PSQL DB.
DbConnnectionString:
Type: String
Default: postgresql://<uname>:<pwd>#x.tcp.ngrok.io:PORT/DB
This is how I read the URI in my Python code
# config.py
DATABASE_URL = os.environ.get('DB_URI')
db_engine = create_engine(DATABASE_URL)
db_session = sessionmaker(autocommit=False, autoflush=False,bind=db_engine)
print(f"Configs initialized for {API_V1_STR}")
# app.py
# 3rd party
from fastapi import FastAPI
# Custom
from config.app_config import PROJECT_NAME, db_engine
from models.db_models import Base
print("Creating all database")
Base.metadata.create_all(bind=db_engine)
app = FastAPI(title=PROJECT_NAME)
print("APP created")
In this setup, everything seems to work as expected.
But whenever I replace the DB URL with RDS DB, suddenly the call gets stuck at create all database step as shown in the image below. when this happens the lambda always times out and throws exceptions.
If I run the code locally using uvicorn this error doesn't occur.
Everything works as expected.
When I use sam local invoke even with RDS URL, the API call works without any issues.
This problem occurs only while deployed in AWS Lambda.
I notice that configs are initialized twice in this setup, Once before START request ID and once after.
I have tried reading up on it but not clear what could I do to fix this. Any help would be much appreciated.
It was my bad!. I didn't pay attention to security groups. It was a connection timeout all along. Once I fixed the port access in Security groups, lambda started working as expected.

How do i fix HTTPSConnectionPool - Read Timed Out Error when connecting to database server

I am trying to connect to a FileMaker Databse server via python script, and my code was working before but has suddenly stopped, and i didnt make any changes to the portion of code that no longer works. I am encountering the following error:
Request error: HTTPSConnectionPool(host='**.**.*.*', port=443): Read timed out. (read timeout=30)
I have taken out the code that creates the server instance and connects/logs in, and then logs out without making any changes in the database, and i am still recieving the same error. However, i can connect to the filemaker server and database via the FileMaker applicaiton with no issues, and i can connect to the server using Telnet commands. I am on windows 10 and writing the code in PyCharm CE. I have reinstalled PyCharm, created a new virtual environment, and tried reinstalling the fmrest module, as well as using older versions. I have also increased the timeout time to give more time to login, which hasnt worked. I'm basically stumped on why i can no longer log in via the script, when it has been working perfectly in testing for the past couple weeks. My code is below.
import fmrest
from fmrest.exceptions import FileMakerError
from fmrest.exceptions import RequestException
import sys
import requests
# connect to the FileMaker Server
requests.packages.urllib3.disable_warnings()
fmrest.utils.TIMEOUT = 30
try:
fms = fmrest.Server('https://**.**.*.*',
user = '***',
password = '******',
database = 'Hangtag Order Management',
layout = 'OrderAdmin',
verify_ssl = False)
except ValueError as err:
print('Failed to connect to server. Please check server credentials and status and try again\n\n' + str(err))
sys.exit()
print(fms)
print('Attempting to connect to FileMaker Server...')
try:
fms.login()
print('Login Successful\n')
except FileMakerError as err:
print(err)
sys.exit()
except RequestException as err:
print('There was an error connecting to the server, the request timed out\n\n' + str(err))
sys.exit()
fms.logout()
This should successfully login to the database, print 'login successful' and log out. Calling print(fms) returns
<Server logged_in=False database=Hangtag Order Management layout=OrderAdmin>
but i receive the connection error upon the login attempt. I am assuming the error is server side, but i dont know enough about servers to accurately trouble shoot. Could the server have blacklisted my IP for making so many login attempts during my testing? and if so where would i undo that/prevent it from happening again?
A couple of server reboots fixed the error, not really sure of the ultimate cause.

how to keep running a client program in python which uses Twilio

I have deployed a Flask application on an Ubuntu server. In order to make a check on the Flask application, I have used Twilio, such that the data will be sent to the server from the client every 5 minutes. In case something goes wrong, I should be getting a text message on my phone. Right now I am doing this on my local machine but I want to know how can I make it run always? Do I have to run the below client code on the Ubuntu server or how it could be done?
import json
import requests
def localClient():
try:
data = {"inputData": "Bank of America", "dataId": 12345}
response = requests.post("http://12.345.567.890/inputData", json=data).json()
except:
from twilio.rest import Client
account_sid = "XXXXXXXXXXXXXXX"
auth_token = "XXXXXXXXX"
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body='Server is down',
from_='+12345678901',
to='+19876543210' )
while True:
localClient()
time.sleep(300)
Use supervisor in Ubuntu. This will auto restart your code whenever you restart server. You don't need to start every time. This will run forever until you stop manually.
Refer to the following link to setup supervisor :
supervisor

2 Python 3 Async Clients Simultaneously (discord.py + aiohttp.web)

I have a discord.py bot and I'd like to make it possible to activate a function within the discord bot by sending a GET request to the server.
I found this code elsewhere, but it doesn't work. No errors, but it still runs the clients one after the other.
def runInParallel(*fns):
proc = []
for fn in fns:
p = Process(target=fn)
p.start()
proc.append(p)
for p in proc:
p.join()
How can I have an aiohttp server running without shutting down the discord server?
you can run aiohttp along with any library that works with asyncio.
or you can start aiohttp in separate thread.
I've found a solution of sorts, I had to use an async web server, namely the poorly documented Kyoukai - http://kyoukai.readthedocs.io/en/latest/
I can now silmeoutaneously interface with the Discord API and host a simple web server!

Resources