I'm trying to connect from the one machine to another remote server with installed RabbitMQ.
The RabbitMQ is working perfectly locally, but when I connect to it from the another machine then an error is occurs:
root#xxx:~# python3 rabbitmq.py
Traceback (most recent call last):
File "rabbitmq.py", line 8, in <module>
connection = pika.BlockingConnection(pika.ConnectionParameters(parameters))
File "/usr/local/lib/python3.4/dist-packages/pika/connection.py", line 652, in __init__
self.host = host
File "/usr/local/lib/python3.4/dist-packages/pika/connection.py", line 392, in host
(value,))
TypeError: host must be a str or unicode str, but got <ConnectionParameters host=111.111.111.111 port=5672 virtual_host=product ssl=False>
root#xxx:~#
TypeError: host must be a str or unicode str, but got ConnectionParameters host=111.111.111.111 port=5672
virtual_host=product ssl=False
The Python code on other remote machine:
import pika
credentials = pika.PlainCredentials(username='remoteuser', password='mypassword')
parameters = pika.ConnectionParameters(host='111.111.111.111', port=5672, virtual_host='/', credentials=credentials)
#connection = pika.BlockingConnection(pika.ConnectionParameters('111.111.111.111:15672')) # --- it doesn't work too
connection = pika.BlockingConnection(pika.ConnectionParameters(parameters))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
User "remoteuser" has admin rights and access to virtual host "/"
http://111.111.111.111:15672/#/users
Name Tags Can access virtual hosts Has password
remoteuser administrator / ●
What is the problem?
you have double wrapped parameters, change:
connection = pika.BlockingConnection(pika.ConnectionParameters(parameters))
to:
connection = pika.BlockingConnection(parameters)
Related
I'm currently writing a server application which communicates to clients using websockets. I want to deploy the server in a Docker container. To test the connection, I wrote a client in Python. When running the server without Docker on my local system, the client can connect, send and receive messages without a problem. As soon as I run the server in a Docker container though, the client fails to connect with the following error message:
Traceback (most recent call last): File "./client-ws.py", line 40, in <module>
asyncio.get_event_loop().run_until_complete(loop(msg, sockaddr)) File "/usr/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result() File "./client-ws.py", line 11, in loop
async with websockets.connect(sockaddr) as sock: File "/usr/lib/python3.7/site-packages/websockets/py35/client.py", line 2, in __aenter__
return await self File "/usr/lib/python3.7/site-packages/websockets/py35/client.py", line 20, in __await_impl__
extra_headers=protocol.extra_headers, File "/usr/lib/python3.7/site-packages/websockets/client.py", line 283, in handshake
status_code, response_headers = yield from self.read_http_response() File "/usr/lib/python3.7/site-packages/websockets/client.py", line 92, in read_http_response
raise InvalidMessage("Malformed HTTP message") from exc websockets.exceptions.InvalidMessage: Malformed HTTP message
Here's my client's code:
async def loop(msg, sockaddr):
async with websockets.connect(sockaddr) as sock:
while True:
print("Sending position {}".format(i))
message = '{\"type\" : \"PositionUpdate\",\"position\": {\"latitude\": ' + msg[i][0] +',\"longitude\": ' + msg[i][1] + ',\"elevation\": ' + msg[i][2] + ',\"speed\": ' + msg[i][3] + '}}'
await sock.send(message)
print(await sock.recv())
i += 1
if i >= len(msg):
i = 0
time.sleep(1)
parser = argparse.ArgumentParser()
parser.add_argument('--host', help='The host to which the socket should bind', required=True)
parser.add_argument('--port', help='The port to which the socket should bind', required=True)
parser.add_argument('--file', help='Path to the coordinate resource file. The file should have the following structure: latitude,longitude,elevation,speed;latitude,longitude,...', required=True)
args = parser.parse_args()
file = open(args.file, 'r')
locationsStr = file.read()
locations = locationsStr.split(';')
msg = []
for l in locations:
tmp = l.split(',')
msg.append(tmp)
sockaddr = "ws://" + args.host + ":" + args.port
i = 0
asyncio.get_event_loop().run_until_complete(loop(msg, sockaddr))
On my server, which is written in C++, I use the library websocketpp to open a websocket. I can't post all of the server's code, but here is how I open the websocket:
broadcastServer::broadcastServer(SpatMessageHandler spatHandler, MapMessageHandler mapHandler,
ClearSpatCache spatCclear, ClearMapCache mapCclear) {
this->spatHandler = spatHandler;
this->mapHandler = mapHandler;
this->spatCclear = spatCclear;
this->mapCclear = mapCclear;
m_server.init_asio();
m_server.clear_access_channels(websocketpp::log::alevel::all);
m_server.set_open_handler(bind(&broadcastServer::onOpen, this, ::_1));
m_server.set_close_handler(bind(&broadcastServer::onClose, this, ::_1));
m_server.set_message_handler(bind(&broadcastServer::onMessage, this, ::_1, ::_2));
}
...
void broadcastServer::run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
As far as I can judge, websocketpp binds to 0.0.0.0 by default. I run the docker container with the following command:
sudo docker run -p 9001:9001 -it $dockerhash
EDIT: I've just looked if something's wrong with the websocket in the container. Executing netstat -an inside the container shows, that the websocket didn't even bind to a port.
The issue was that I was running an older version of boost in my Docker container as on my local machine. I solved the issue by installing the boost headers from source.
I've looked at several very similar examples, but I'm doing something wrong...probably because I'm mixing up local or remote binding addresses or well, not sure. I've yet to find a document that can describe what each is supposed to be to a newb like me.
I have a raspberry pi in a robot which has MariaDB installed. I can connect to the server from my PC with SQL Workbench.
I have a second Pi that needs a python script that can send data to the first pi...
IP Addresses, names and passwords have been changed to protect the innocent, but the whole thing is a closed network anyways.
import mysql.connector
import sshtunnel
_host = Robot's IP Address
_ssh_port = 22
_username = Robot user login
_password = Robot Password
_remote_bind_address = Robot's IP Address
_remote_mysql_port = 3308
_local_bind_address = Second Pi's IP Address
_local_mysql_port = 3308
_db_user = Database User Name
_db_password = Database Password
_db_name = "joycap"
with sshtunnel.SSHTunnelForwarder(
(_host, _ssh_port),
ssh_username=_username,
ssh_password=_password,
remote_bind_address=(_remote_bind_address, _remote_mysql_port),
local_bind_address=(_local_bind_address,_local_mysql_port)
) as tunnel:
connection = mysql.connector.connect(
user=_db_user,
passwd=_db_password,
host=_local_bind_address,
database=_db_name,
port=_local_mysql_port)
Here's the error I'm current getting...
pi#raspberrypi:~ $ python dbtest1.py
2018-04-05 06:11:42,262| ERROR | Secsh channel 0 open FAILED: Connection refused: Connect failed
2018-04-05 06:11:42,277| ERROR | Could not establish connection from ('Second Pi's IP Address', 3308) to remote side of the tunnel
Traceback (most recent call last):
File "dbtest1.py", line 29, in <module>
port=_local_mysql_port)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 184, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 100, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/abstracts.py", line 733, in connect
self._open_connection()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 241, in _open_connection
self._do_handshake()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 108, in _do_handshake
packet = self._socket.recv()
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", line 248, in recv_plain
raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
Would appreciate any advice! Would think someone would have made a video on doing this by now :)
Regards,
Matt
Finally worked it out through trial and error.
_host = "192.168.8.1" #Server where database is hosted
_ssh_port = 22 #ssh port
_username = "pi" #user name to host server
_password = "raspberry" #password to host server
_remote_bind_address = "127.0.0.1" #local connection IP to db on host(should probably not change)
_remote_mysql_port = 3306
_local_bind_address = "0.0.0.0" #local bind address(should probably not change)
_local_mysql_port = 3306
_db_user = "databaseuser"
_db_password = "databasepass"
_db_name = "databasename"
local-host --->Aterm server (security server ) -----> target-machine(
I am trying to write a code in Python using Paramiko to first SSH from local-host to the target-machine. From the target-machine, I want to capture some outputs and store them locally either as a variable or as a file (havent got to that point yet). I found an example from stackoverflow where they talk about using nested SSH with paramiko, and I follow it but I get stuck here:
i need just reaching the target-machine
My code:
import paramiko
import sys
import subprocess
hostname = '10.10.10.1'
port = 22
username = 'mohamed.hosseny'
password ='Pass#1'
client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)
client.close()
but i found the below error message :
Traceback (most recent call last):
File "C:/Users/mohamed.hosseny/Desktop/Paramiko.py", line 13, in <module>
client = paramiko.Transport((hostname, port))
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 332, in
__init__
'Unable to connect to {}: {}'.format(hostname, reason))
SSHException: Unable to connect to 10.10.10.1: [Errno 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
paramiko.Transport is a lower level API. Don't use it unless you have a good reason. Instead, you can use paramiko.SSHClient.
I have my RabbitMQ Server running on AWS EC2
I have run the producer and consumer code locally. It is working.
I am able to access the rabbitMQ management web app as well.
When I am trying to push data from my laptop to EC2
I am getting this error on this line:
connection = pika.BlockingConnection(pika.ConnectionParameters('xx.xx.xx.xx',5672,'/',credentials))
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 339, in init
self._process_io_for_connection_setup()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 374, in _process_io_for_connection_setup
self._open_error_result.is_ready)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 395, in _flush_output
raise exceptions.ConnectionClosed()
pika.exceptions.ConnectionClosed
where xx.xx.xx.xx: public IP address of my instance
Pls tell me, if I am using the correct parameters. What should be the IP address , virtual hostname.
I have checked the credentials , the user that I am using exists and it has the rights to access '/' virtual host
I have made the needed changes in the Security Groups.
This is a screenshot of it:
When I am running the same producer code from within the instance it's working properly. No Exceptions and the consumer is able to receive it as well.
This is my complete code for reference:
import pika
print("Start")
credentials=pika.PlainCredentials('manish','manish')#RabbitMQ user created on EC2
connection=pika.BlockingConnection(pika.ConnectionParameters('xx.xx.xx.xx',5672,'/',credentials))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
I tried socket_timeout and it worked for me, you could try something like :
credentials = pika.PlainCredentials('username,'password')
connection = pika.BlockingConnection(pika.ConnectionParameters('hostname',port,'virtual host',credentials,**socket_timeout=10000**))
I am trying to connect to a Meteor Mongo database through pymongo. Here's the code:
def get_mongo_url(site):
# return "mongodb://client-xxxxx:yyyyy#production-db-c1.meteor.io:27017/site"
import subprocess
p = subprocess.Popen(['meteor', 'mongo', '--url', site], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out
return out
from pymongo import MongoClient
client = MongoClient(get_mongo_url("mysite.com"))
And the error (the print statement yields a correct url)
>> mongodb://client-xxxxx:yyyyy#production-db-c1.meteor.io:27017/site
Traceback (most recent call last):
File "private/test.py", line 46, in <module>
client = pymongo.MongoClient(get_mongo_url(METEOR_SITE))
File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 369, in __init__
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-xxxxx'), ('nonce', u'zzzzz'), ('key', u'ttttt')]) failed: auth fails
If I run meteor mongo --url mysite.com, copy the result into the return ... at the top of the function and uncomment it, the connection works. Why can't I connect programmatically?
The subprocess code appends a line feed character \n to the end of the url.
You need to strip that with .rstrip()
The right way to do that is to replace the return in your function with
return out.rstrip()
For confirmation purposes I will show what happens with the function as-is and
rstrip() applied/unapplied to the return.
murl = get_mongo_url('').rstrip()
mongodb://client-faf1d0db:746d8f43-367b-dde2-b69a-039ff8b9f7fa#production-db-a1.meteor.io:27017/_meteor_com
client = pymongo.MongoClient(murl)
Worked OK
murl = get_mongo_url('')
mongodb://client-3578a20b:d4ddeec9-6d24-713e-8ddb-c357b664948a#production-db-a1.meteor.io:27017/_meteor_com
client = pymongo.MongoClient(murl)
Traceback (most recent call last):
File "", line 1, in
File "/home/action/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 383, in init
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-3578a20b'), ('nonce', u'e14e2bdb3d8484b9'), ('key', u'9
c101b78ff1a617a9c5f0def36c7e3d9')]) failed: auth fails
Failed without the rstrip.
murl = get_mongo_url('')
mongodb://client-1a193a61:4c9c572e-22e3-4b7e-44a1-dc76bfb65e86#production-db-a1.meteor.io:27017/_meteor_com
client = pymongo.MongoClient(murl)
Traceback (most recent call last):
File "", line 1, in
File "/home/action/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 383, in init
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-1a193a61'), ('nonce', u'a2576142b1a33d8b'), ('key', u'4
419c490bcdcc65b20f2950c3b106d59')]) failed: auth fails
Failed again (no rsrtip)
murl = get_mongo_url('').rstrip()
mongodb://client-ce463608:d7dc6be0-499f-1808-43e1-fdfb8b6e8ebc#production-db-a1.meteor.io:27017/_meteor_com
client = pymongo.MongoClient(murl)
Worked (rstrip used).
The following is general info on mongodb URLs. You may know this already.
The URL that pymongo wants is not a web URL but a URL-like specifier for a mongo database connection.
For a development environment, the mongodb is usually set up on port 3001, which is not the default mongodb port for a production server.
Meteor applications can be configured to use a mongodb hosted anywhere. It does not have to be on the same server that serves the meteor content. The specification of this is done through the mongodb:// URL which is what pymongo wants. pymongo doesn't depend on the meteor website url, which can be very different from the mongodb url.
Here is some code I am using
import pymongo
MONGO_URL = r'mongodb://localhost:3001/meteor'
###
def connect():
client = pymongo.MongoClient(MONGO_URL)
return client
def findUser(c, email):
users = c.meteor.users
return users.find_one({"emails.address": email})
According to the mongodb site on Github, The MONGO_URL format is
mongodb://[username:password#]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
so the mongodb url mongodb://localhost:3001/meteor can be interpreted like this:
* mongodb:// means this describes a mongodb connection
* localhost means connect locally
* :3001 means use non-standard port number 3001. this is how "meteor run" sets up mongo
* /meteor means connect to the database called "meteor"