Can not get remote access to RabbitMq server - python

I can't get remote access to the RabbitMq server. When I am doing this at one machine and connect to localhost:15672 it works, but when I try from a remote machine it's not.
I have already created a user with all permissions and check that 5672 port on computer works (just tried to connect to him from a remote computer). Also, I tried to connect to 15672 port, but no result.
credentials = pika.PlainCredentials(username= 'username', password='password')
parameters = pika.ConnectionParameters(host='188.19.100.226',
port=5672,
virtual_host= '/',
credentials=credentials)
connection = pika.BlockingConnection(parameters=parameters)
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body=hello)
print ("Sent!")
connection.close()
I expect to see Sent!, but instead of I get:
ERROR:pika.adapters.utils.io_services_utils:Socket failed to connect: <socket.socket fd=7, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('192.168.100.2', 44752)>; error=111 (Connection refused)
ERROR:pika.adapters.utils.connection_workflow:TCP Connection attempt failed: ConnectionRefusedError(111, 'Connection refused'); dest=(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('188.19.100.226', 5672))
ERROR:pika.adapters.utils.connection_workflow:AMQPConnector - reporting failure: AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused')
ERROR:pika.adapters.utils.connection_workflow:AMQP connection workflow failed: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused'); first exception - None.
ERROR:pika.adapters.utils.connection_workflow:AMQPConnectionWorkflow - reporting failure: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused'); first exception - None
ERROR:pika.adapters.blocking_connection:Connection workflow failed: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused'); first exception - None
ERROR:pika.adapters.blocking_connection:Error in _create_connection().
Traceback (most recent call last):
File "/home/roman/PycharmProjects/trrp2/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
Traceback (most recent call last):
File "/home/roman/PycharmProjects/trrp2/send.py", line 52, in <module>
connection = pika.BlockingConnection(parameters=parameters)
File "/home/roman/PycharmProjects/trrp2/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 359, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/home/roman/PycharmProjects/trrp2/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError

You need to configure rabbitmq to listen to other interfaces than local host.
To accept connections from all interfaces, listen to 0.0.0.0
https://www.rabbitmq.com/networking.html#interfaces

Related

How to Handel exception during Mysql connection

I m trying to connect to a mysql database. if server is not responding, my application crashes. I am using try, except but looks like two exceptions are raising and "try: except" could not handle it. can some one figure it out where is the problem. below is my code:-
def check_server(server_address):
con = mysql.connector.connect(host='{}'.format(server_address),
database='domicile_reports',
user='xyz',
password='xyz')
try:
if con.is_connected():
print('{} Connected'.format(server_address))
con.close()
except Exception as e:
print("Can not connect to db. {} Occured".format(e))
check_server('25.13.253.67')
Error displayed on terminal:-
Traceback (most recent call last):
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector
network.py", line 574, in open_connection
self.sock.connect(sockaddr)
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
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "f:\Docs\OneDrive\Python Projects\CFC App\splash_screen_gui.py", line 151, in
obj = splashscreen()
File "f:\Docs\OneDrive\Python Projects\CFC App\splash_screen_gui.py", line 51, in init
self.check_server(self.server_2)
File "f:\Docs\OneDrive\Python Projects\CFC App\splash_screen_gui.py", line 80, in check_server
con = mysql.connector.connect(host='{}'.format(server_address),
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector_init_.py", line 273, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector\connection.py", line 116, in init
self.connect(**kwargs)
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector\abstracts.py", line 1052, in connect
self._open_connection()
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector\connection.py", line 494, in _open_connection
self._socket.open_connection()
File "C:\Users\Hamid Shah\AppData\Roaming\Python\Python310\site-packages\mysql\connector\network.py", line 576, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '25.13.253.67:3306' (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)
You need to put the connect() call inside the try: except block. You can also use a context manager to properly close the connection, example:
import mysql.connector
from mysql.connector.errors import Error
def check_server(server_address):
try:
with mysql.connector.connect(
host=server_address,
database="domicile_reports",
user="xyz",
password="xyz",
) as cnx:
print(f"{server_address} Connected")
except Error as err:
print(f"Can not connect to db. {err} Occured")
check_server("25.13.253.67")

can't remotely connect to mysql database in cpanel with python

I tried to connect remotely to my database in cpanel in all ways but it won't let me, does anyone know why it doesn't give me access?
HOST = "xxxxxxxxx"
DATABASE = "xxxxxxxxx"
USER = "xxxxxxxxxxx"
PASSWORD = "xxxxxxx"
db_connection = mysql.connect(host=HOST, database=DATABASE, user=USER, password=PASSWORD)
print("Connected to:", db_connection.get_server_info())
#####################################################################################
python3 sqlConect.py
Traceback (most recent call last):
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/network.py", line 509, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/miguel/Desktop/python/sqlConect.py", line 61, in <module>
db_connection = mysql.connect(host=HOST, database=DATABASE, user=USER, password=PASSWORD)
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 716, in connect
self._open_connection()
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/connection.py", line 206, in _open_connection
self._socket.open_connection()
File "/home/miguel/.local/lib/python3.10/site-packages/mysql/connector/network.py", line 511, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'industriasbjt.com:3306' (111 Connection refused)
In cpanel when you try to connect remotely and access is denied by port 3306, you have to contact the cpanel provider and check the plans that give remote access, because if port 3306 is blocked the remote connection plan may is limited, it's just a matter of talking to the provider that provides the cpanel

Python myqsl.connector connection error on pyton.org

I have a mysql server on my computer. I did port fowarding on my modem. I checked port is open or not with signal sites and return success answer. When i try connect on my pc with host = "localhost" i returned success answer. But when i try connect mysql server with python.org's shell, i returned connection error same like this:
Traceback (most recent call last):
File “/usr/lib/python3.8/site-packages/mysql/connector/network.py”, line 529, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python3.8/site-packages/mysql/connector/init.py”, line 177, in connect
return MySQLConnection(*args, **kwargs)
File “/usr/lib/python3.8/site-packages/mysql/connector/connection.py”, line 104, in init
self.connect(**kwargs)
File “/usr/lib/python3.8/site-packages/mysql/connector/abstracts.py”, line 781, in connect
self._open_connection()
File “/usr/lib/python3.8/site-packages/mysql/connector/connection.py”, line 284, in _open_connection
self._socket.open_connection()
File “/usr/lib/python3.8/site-packages/mysql/connector/network.py”, line 531, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can’t connect to MySQL server on ‘myipadress:3306’ (111 Con
nection refused)
How can i connect my mysql server ?
In order to encounter this error first do the following:
Make sure the host is in the format 127.0.0.1 and there are no / or \ in the host name
Check if you have specified all the correct info about the server like passwords, user, root, etc
Make sure the server is active and there is no fire wall blocking your access
Remove all the unwanted users from the server

Socket Exception couldn't reconnect to the network in python

when the network goes offline, the sockets throws error instead of reconnecting. It is reconnecting but not trying to achieve the connection. throws error in second or third attempt. Do I need to increase the wait time or is there anything which can be added to make sure the reconnect takes place
import socket,time
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ip = '192.168.xx.x'
port = 4196
address = (ip,port)
client.connect(address)
print("connecting")
while 1:
try:
client.send(b'\x01\x04xxxxxxx')
print("sent")
data = client.recv(1024)
print(data)
time.sleep(5)
except socket.error:
while 1:
print("error")
client.close()
time.sleep(30)
print("reconnecting")
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
while 1:
client.connect(address)
print("connected back")
break
break
OutPut:
connecting
sent
01 04 xxxxxxxxx
error
reconnecting
Error:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 15, in <module>
client.send(b'\x01\x04\xxxxxxxxxxx')
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Data\pwr\TCP.py", line 28, in <module>
client.connect(address)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

python xmpp simple client error

I'm getting the following error:
AttributeError: Client instance has no attribute 'Dispatcher'
while running the following code in python 2.7:
import xmpp
user= 'uname#gmail.com'
password="pass"
jid = xmpp.JID(user)
connection = xmpp.Client(jid.getDomain())
connection.connect()
connection.auth(jid.getNode(),password)
Would be happy if someone knows how to fix it.
P.S. Full traceback of the error after the fix proposed by N3RO:
C:\Users\krasnovi\Desktop\temp\xmpp tests>python xmpp.client.py
Invalid debugflag given: always
Invalid debugflag given: nodebuilder
DEBUG:
DEBUG: Debug created for build\bdist.win-amd64\egg\xmpp\client.py
DEBUG: flags defined: always,nodebuilder
DEBUG: socket start Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027C1708> into <xmpp.client.Client instance at 0x00000000027C1588>
DEBUG: socket warn An error occurred while looking up _xmpp-client._tcp.t
alk.gmail.com
DEBUG: socket error Failed to connect to remote host ('talk.gmail.com', 52
23): getaddrinfo failed (11004)
Traceback (most recent call last):
File "build\bdist.win-amd64\egg\xmpp\transports.py", line 133, in connect
self._sock.connect((server[0], int(server[1])))
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
gaierror: [Errno 11004] getaddrinfo failed
DEBUG: socket stop Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027C1708> out of <xmpp.client.Client instance at 0x00000000027C1588>.
Traceback (most recent call last):
File "xmpp.client.py", line 11, in <module>
connection.auth(jid.getNode(),password)
File "build\bdist.win-amd64\egg\xmpp\client.py", line 214, in auth
AttributeError: Client instance has no attribute 'Dispatcher'
Before the fix:
Invalid debugflag given: always
Invalid debugflag given: nodebuilder
DEBUG:
DEBUG: Debug created for build\bdist.win-amd64\egg\xmpp\client.py
DEBUG: flags defined: always,nodebuilder
DEBUG: socket start Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027ED708> into <xmpp.client.Client instance at 0x00000000027ED588>
DEBUG: socket error Failed to connect to remote host ('xmpp.l.google.com.'
, 5222): A connection attempt failed because the connected party did not properl
y respond after a period of time, or established connection failed because conne
cted host has failed to respond (10060)
Traceback (most recent call last):
File "build\bdist.win-amd64\egg\xmpp\transports.py", line 133, in connect
self._sock.connect((server[0], int(server[1])))
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10060] A connection attempt failed because the connected party did
not properly respond after a period of time, or established connection failed b
ecause connected host has failed to respond
DEBUG: socket stop Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027ED708> out of <xmpp.client.Client instance at 0x00000000027ED588>.
Traceback (most recent call last):
File "xmpp.client.py", line 11, in <module>
connection.auth(jid.getNode(),password)
File "build\bdist.win-amd64\egg\xmpp\client.py", line 214, in auth
AttributeError: Client instance has no attribute 'Dispatcher'
You need to specify a server you want to connect to.
connection.connect(server=('serveradress.com', portnumber))
After changing this I couldn't reproduce the AttributeError.
I would also advise you to use a correct JID to test your code.
JID's are like E-Mails separated by an #, which is why in your sample code jid.getNode() returns nothing.
*Edit my Code example:
import xmpp
user = 'username#gmail.com'
password = 'password'
jid = xmpp.JID(user)
connection = xmpp.Client(jid.getDomain())
connection.connect(server=('talk.google.com', 5223))
connection.auth(jid.getNode(), password)
connection.sendInitPresence()
In your traceback it looks like you are trying to connect to talk.gmail.com which is a non existant domain, so the connection.connect statement will fail to open a connection.
Try to connect to talk.google.com which may be the right server's name.

Categories