python unable to connect to mqtt broker - python

When I was trying out a tutorial in the internet, I failed to connect to the mqtt broker - anyone can help me on that?
import paho.mqtt.client as mqtt
broker_url = "mqtt.eclipse.org"
broker_port = 1883
client = mqtt.Client()
client.connect(broker_url, broker_port)
print(client)
Traceback (most recent call last):
File "C:\Workspace\FI Systemintegration\Python\MitarbeiterVerwaltung\rpi\connect.py", line 6, in <module>
client.connect(broker_url, broker_port)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 914, in connect
return self.reconnect()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

The service does not exist at mqtt.eclipse.org. It was either used only as an example or the service is no longer available.
You should use another MQTT instance, maybe run a local instance instead.

Related

Fabric connection to an extender

I wanted to connect to a extender for a Wi-Fi project, but I don't know if I can connect to an extender using the Fabric module for Python...
I was trying to connect to a server using it's name, but I'm sure that the name is wrong, but I don't know if I'm connecting wrong. Here is my code:
from fabric import Connection
node = Connection('KIN-AP23')
letsgo = node.run('help')
I don't know how to connect since I got this error:
Traceback (most recent call last):
File "main.py", line 5, in <module>
letsgo = node.run('help')
File "<decorator-gen-3>", line 2, in run
File "/home/runner/FLIPPER1/venv/lib/python3.8/site-packages/fabric/connection.py", line 29, in opens
self.open()
File "/home/runner/FLIPPER1/venv/lib/python3.8/site-packages/fabric/connection.py", line 636, in open
self.client.connect(**kwargs)
File "/home/runner/FLIPPER1/venv/lib/python3.8/site-packages/paramiko/client.py", line 349, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/home/runner/FLIPPER1/venv/lib/python3.8/site-packages/paramiko/client.py", line 203, in _families_and_addresses
addrinfos = socket.getaddrinfo(
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
I know for sure that's not the right name but is there any extra stuff I have to do, if so tell me, if not still tell me how to do it!

How to connect to my Microsoft mail using python to retrieve mail?

I'm unable to connect to my email.
Some information about Microsoft IMAP sever name, host, encryption I got from this link https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
Is there something wrong with my code?
This is my code
import imaplib
import pprint
imap_host = 'outlook.office365.com:993'
imap_user = 'myemail#hotmail.com'
imap_pass = 'password'
imap = imaplib.IMAP4(imap_host) # code dosen't execute past this line. I guess it has something to do with SSL or TSL
## login to server
imap.login(imap_user, imap_pass)
imap.select('Inbox')
tmp, data = imap.search(None, 'ALL')
for num in data[0].split():
tmp, data = imap.fetch(num, '(RFC822)')
print('Message: {0}\n'.format(num))
pprint.pprint(data[0][1])
break
imap.close()
This is the error message I'm getting
File "C:\Users\User\Documents\Python project\email-server\getemail.py", line 9, in <module>
imap = imaplib.IMAP4(imap_host)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 202, in __init__
self.open(host, port, timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 312, in open
self.sock = self._create_socket(timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 302, in _create_socket
return socket.create_connection(address)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

How to get data from The Things Network through python paho mqtt library

I am getting an error when i try to use the attached python code to connect and send messages to the Things Network application. When i try using the mqtt mosquitto broker in the terminal with
Traceback (most recent call last):
File "/Users/ayushsharma/Desktop/ttn.py", line 28, in <module>
client.connect(broker,port = 8883)
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
it works in connecting but not through the python script. I'm really new to all this so i appreciate any feedback or guidance on how to approach this problem of retrieving date from TheThingsNetwork.
#!/usr/bin/env python3
import paho.mqtt.client as paho
#settings
app_id = "appID"
access_key = "access_key"
broker="nam1.cloud.thethings.network "
port=1883
def on_publish(client,userdata,result):
print("data published \n")
print(client,userdata,result)
pass
def on_log(mqttc, obj, level, string):
print(string)
#setup
client = paho.Client()
client.username_pw_set(app_id,access_key)
#callbacks
client.on_publish = on_publish
client.on_log = on_log
#establish connection
client.connect(broker,port = 8883)
client.loop_start()
#publish
ret= client.publish("appID","on",qos=1)
ret.wait_for_publish()
[1]: https://i.stack.imgur.com/2oejU.png

How can I connect to a FTP server in Python? [duplicate]

This question already has an answer here:
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known [duplicate]
(1 answer)
Closed 1 year ago.
I am having problems when I try to connect to a ftp server, I am using a Mac with BigSur and Python3.
Here is my code:
import ftplib
FTP_HOST = "ftp://myserver"
FTP_USER = "user"
FTP_PASS = "pass"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
Here what it returns:
Traceback (most recent call last):
File "/Users/adrianrodriguezgalisteo/jonsuGo/test_upload.py", line 8, in <module>
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 119, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 156, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I just want to connect to the server and upload a file.
Thank you.
https://docs.python.org/3/library/ftplib.html
The host should only be myserver
FTP_HOST = "myserver"
You may need the FQDN of the servername

Send email using python

I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?
But, my the mails that I sent do not reach the addresses.
This is the error that I get:
Traceback (most recent call last):
File "something.py", line 24, in <module>
server = smtplib.SMTP('myserver')
File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
What should I be doing here?
What you've get is a DNS query error indicating that domain myserver does not exist.
You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.
This is how I do it.
import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email#gmail.com','your_password')
server.sendmail('your_email#gmail.com','your_email#gmail.com','test email')

Categories

Resources