Telethon bot not working on PythonAnywhere - python

I tried to deploy my Telethon bot on PythonAnywhere but got following exception:
Attempt 1 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 2 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 3 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 4 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 5 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Attempt 6 at connecting failed: ConnectionRefusedError: [Errno 111] Connect call failed ('149.154.167.51', 443)
Traceback (most recent call last):
File "/home/hellbowe/popa3/main.py", line 51, in <module>
clientg.start(phone_tg, password_tg)
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/client/auth.py", line 133, in start
else self.loop.run_until_complete(coro)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/client/auth.py", line 140, in _start
await self.connect()
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/client/telegrambaseclient.py", line 525, in connect
if not await self._sender.connect(self._connection(
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/network/mtprotosender.py", line 127, in connect
await self._connect()
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/network/mtprotosender.py", line 253, in _connect
raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)
It works fine on my device.
I tried to add proxy to TelegramClient() thing but got this:
Traceback (most recent call last):
File "/home/hellbowe/popa3/main.py", line 51, in <module>
clientg = TelegramClient('popa3', api_id_tg, api_hash_tg, proxy = ('http', '***', 80))
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/client/telegrambaseclient.py", line 292, in __init__
session.set_dc(
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/sessions/sqlite.py", line 168, in set_dc
self._update_session_table()
File "/home/hellbowe/.local/lib/python3.9/site-packages/telethon/sessions/sqlite.py", line 194, in _update_session_table
c.execute('delete from sessions')
sqlite3.OperationalError: database is locked

You have some other process that has locked your sqlite database (or the database is corrupted) Try deleting the database file or moving it somewhere else so that it can be recreated.

As far as I know, you need to log in into Telegram to use telethon. In your machine you can do it with command line interface, but in hosting you can't. I solved this problem when rent a virtual server, where I can log in Telegram. Then I just run python script with some options in order to always working

Related

Getting timeout error while using smtplib library to send email

Here is my code:
import smtplib
connection = smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user="mymail#gmail.com", password="mypassowrd")
connection.sendmail(from_addr="mymail#gmail.com", to_addrs="recievermail#gmail.com", msg="Hello")
connection.close()
So I am getting this error:
Traceback (most recent call last):
File "E:\100Days-Python_programs\Day32\Birthday Wisher (Day 32) start\main.py", line 3, in <module>
connection = smtplib.SMTP("smtp.gmail.com")
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process finished with exit code 1
I turned on Less secure app access:
I turned off every security steps too:
And I also turned off the firewall protection as well.
But Nothing worked.
So please someone help me.
You need to specify the port. In this case, it's 587 for TSL.
Somehow it works, but I don't have profound knowledge to explain why.
I had the same problem, so there is a solution:
connection = smtplib.SMTP("smtp.gmail.com", 587)

Trouble connecting to Cloud SQL in python

I am trying to connect to a database on Cloud SQL, but I keep getting the same error. Not sure what it is, and tried several approaches.
Input:
import pymysql
connection = pymysql.connect(host='127.0.0.1',
user='',
password='XXXX',
db='cmcsql')
output:
C:\Users\Ejer\anaconda3\envs\pythonProject\python.exe C:/Users/Ejer/PycharmProjects/pythonProject/CloudSQL_test.py
Traceback (most recent call last):
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 569, in connect
sock = socket.create_connection(
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Der kunne ikke oprettes forbindelse, fordi destinationscomputeren aktivt nægtede det
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Ejer/PycharmProjects/pythonProject/CloudSQL_test.py", line 2, in <module>
connection = pymysql.connect(host='127.0.0.1',
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 327, in __init__
self.connect()
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 619, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([WinError 10061] Der kunne ikke oprettes forbindelse, fordi destinationscomputeren aktivt nægtede det)")
Process finished with exit code 1
You should not be using 127.0.0.1 to connect to cloud sql instance.
127.0.0.1 is (in most cases) for localhost connection (when you run the db server locally on the same machine as where the client is). Instead you should be using the ip address given to your cloud sql instance. You can find it on the overview tab of the sql instance under Connect to this instance.
You should also create your user on the users tab and then use it in your code.
Don't forget about authentication, check out connections tab and read more about it here
Making sure that you have proper ip, user, existing db and connecting from authorized network should do the trick.
PyMysql connects to port 3306 by default, if your process is running on a different port it won't be able to connect.
Apart from the host, user, password parameters you also have to provide the port on which the sql process is running.

Python Simplemonitor does not send out email on alert; possible configuration error

I'm setting up simplemonitor, found here, to have a check for the urls of my webservice. If any check fails, then it should send out an email alert.
So far, I've confirmed that the monitor works properly. However, when I shut down the service to check the email alert, it errors on sending an email:
2020-04-10 20:03:00 WARNING (simplemonitor) monitor failed but within tolerance: test-check
2020-04-10 20:03:10 ERROR (simplemonitor) monitor failed: test-check (Requests exception while opening URL: HTTPConnectionPool(host='www.test.com', port=8080): Max retries exceeded with url: /hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fef7c8762e8>: Failed to establish a new connection: [Errno 111] Connection refused',)))
2020-04-10 20:03:10 ERROR (simplemonitor.alerter-email) couldn't send mail
Traceback (most recent call last):
File "/anaconda/envs/test/envs/alertenv/lib/python3.5/site-packages/simplemonitor/Alerters/mail.py", line 127, in send_alert
server = smtplib.SMTP(self.mail_host, self.mail_port)
File "/anaconda/envs/test/envs/alertenv/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda/envs/test/envs/alertenv/lib/python3.5/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda/envs/test/envs/alertenv/lib/python3.5/smtplib.py", line 307, in _get_socket
self.source_address)
File "/anaconda/envs/test/envs/alertenv/lib/python3.5/socket.py", line 712, in create_connection
raise err
File "/home/user/anaconda3/envs/test/lib/python3.5/socket.py", line 703, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Here is the monitor.ini file to set up the rules:
[monitor]
monitors=/home/user/monitor/monitors.ini
interval=10
[reporting]
loggers=logfile
alerters=email
[logfile]
type=logfile
filename=monitor.log
only_failures=1
[email]
type=email
host=host.domain.com
from=simplemonitor#company.com
to=user#company.com
And the monitors.ini file that defines what I'm monitoring:
[test-check]
type=http
url=http://www.test.com:8080/hello
tolerance=1
I'm running it with simplemonitor --config monitor.ini &>> monitor.log &.
Given that I only started using this, I'm not sure if this is due to a error in the code, or one on my part due to a mistake in setup.
EDIT: I feel silly. The bug was due to a typo in the monitor.ini file. I'd misspelled the name of the smtp server in the host variable. It now sent an email. I apologize for any bother.
As I said in an edit to the original question, this was due to a typo in the ini file for the smtp server name, in host. I apologize for any bother this caused.

Does neo4j desktop include a usable neo4j database

I have installed neo4j desktop and I am able to use neo4j database from it. The problem is when I try to connect to neo4j database from a Django project.
I have configured my settings.py with config.DATABASE_URL = 'bolt://neo4j:neo4j#localhost:7687', but when I run neomodel_install-labels, I get this error:
Connecting to bolt://neo4j:neo4j#localhost:7687
Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/bolt/connection.py",
line 578, in _connect
s.connect(resolved_address)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/bin/neomodel_install_labels", line 67, in <module>
main()
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/bin/neomodel_install_labels", line 62, in main
db.set_connection(bolt_url)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neomodel/util.py", line 65,
in set_connection
max_pool_size=config.MAX_POOL_SIZE)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/v1/api.py", line 94,
in driver
return Driver(uri, **config)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/v1/api.py", line 133,
in __new__
return subclass(uri, **config)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/v1/direct.py", line 7
3, in __new__
pool.release(pool.acquire())
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/v1/direct.py", line 4
4, in acquire
return self.acquire_direct(self.address)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/bolt/connection.py",
line 453, in acquire_direct
connection = self.connector(address, self.connection_error_handler)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/v1/direct.py", line 7
0, in connector
return connect(address, security_plan.ssl_context, error_handler, **config)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/bolt/connection.py",
line 707, in connect
raise last_error
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/bolt/connection.py",
line 697, in connect
s = _connect(resolved_address, **config)
File "/Users/hugovillalobos/Documents/Code/AttractoraProject/AttractoraVenv/lib/python3.6/site-packages/neo4j/bolt/connection.py",
line 587, in _connect
raise ServiceUnavailable("Failed to establish connection to {!r} (reason {})".format(resolved_address, error.errno))
neo4j.exceptions.ServiceUnavailable: Failed to establish connection to ('::1', 7687, 0, 0) (reason 61)
I know the database is running because I can connect from neo4j desktop, and I have installed neo4j-driver and neomodel. I don't know what I am missing.
Ok, I don't know if this apply to any case, and I don't know the reason for it, but I found that it was a matter of spacial characters in the password. I set my password with an exclamation character, and when I was trying to connect, it failed. I changed the password for the user in Neo4j removing the exclamation character and Bingo! I got a connection. Can anybody tell my why this happens?

ConnectionRefusedError: [Errno 111] Connection refused

I was trying to use ftp and I am getting the following error:
>>> ftp = ftplib.FTP('192.168.248.108')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ftplib.py", line 118, in __init__
self.connect(host)
File "/usr/lib/python3.5/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I was trying to take it step by step since the whole client and server codes were not running. Help please. Thank you.
EDIT:
This is the client side code:
from ftplib import FTP
ftp = FTP('192.168.248.128')
ftp.login(user='username', passwd = 'password')
ftp.cwd('/Desktop/FTP')
def placeFile():
filename = 'myfile.txt'
ftp.storbinary('STOR '+filename, open(filename, 'rb'))
ftp.quit()
placeFile()
First of all check this ip to see if ftp service is available, and if it is check the port that it is listening on, cause it maybe (rare but possible) is configured to listen on a different port than the standard one - 21 . Also maybe the connection is blocked by a firewall, and that is why connection gets refused.
Also haven't seen the whole code of yours but I think another/different problem is this: def placeFile() should be changed to this instead def placeFile(ftp) - cause the function placeFile doesn't really know that ftp references to the ftp client you created above.

Categories

Resources