Python websockets error gaierror - python

I'm trying to write a websocket that connects to a service running on localhost but it's throwing an error
>>> from websocket import create_connection
>>> ws = create_connection("ws://127.0.0.1", http_proxy_port="2974", http_proxy_host="quividicontent")
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
ws = create_connection("ws://127.0.0.1", http_proxy_port="2974", http_proxy_host="quividicontent")
File "C:\Python27\lib\websocket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "C:\Python27\lib\websocket\_core.py", line 211, in connect
options.pop('socket', None))
File "C:\Python27\lib\websocket\_http.py", line 64, in connect
hostname, port, is_secure, proxy)
File "C:\Python27\lib\websocket\_http.py", line 97, in _get_addrinfo_list
addrinfo_list = socket.getaddrinfo(phost, pport, 0, 0, socket.SOL_TCP)
gaierror: [Errno 11001] getaddrinfo failed
>>> ws = create_connection("ws://127.0.0.1", http_proxy_port="2974")
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
ws = create_connection("ws://127.0.0.1", http_proxy_port="2974")
File "C:\Python27\lib\websocket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "C:\Python27\lib\websocket\_core.py", line 214, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "C:\Python27\lib\websocket\_handshake.py", line 65, in handshake
status, resp = _get_resp_headers(sock)
File "C:\Python27\lib\websocket\_handshake.py", line 122, in _get_resp_headers
raise WebSocketBadStatusException("Handshake status %d", status)
WebSocketBadStatusException: Handshake status 200
>>> import socket
>>> socket.getaddrinfo('localhost', 2974)
[(23, 0, 0, '', ('::1', 2974, 0, 0)), (2, 0, 0, '', ('127.0.0.1', 2974))]
I'm sure the port is open and listening, used sockets and a more complex example with the onOpen onMessage function works.

Your issue occurs because you need to specify a protocol for the handshake with the websocket.
From wikipedia :
To establish a WebSocket connection, the client sends a WebSocket
handshake request, for which the server returns a WebSocket handshake
response, as shown in the example below.
During this handshake, the server and the client discuss about what protocols should be used and if there is no protocol specified, this could lead to an error (not always).
From the python-websocket doc, specify a protocol can be done with
:
ws = websocket.create_connection("ws://exapmle.com/websocket", subprotocols=["binary", "base64"])
In your particular case, subprotocols should be ['quividicontent']

Related

python unable to connect to mqtt broker

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.

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)

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')

WebSocketBadStatusException: Handshake status 307

I am trying to connect to a SignalR hub from python client using "signalr-client-0.0.7".In connection.start() its giving "WebSocketBadStatusException: Handshake status 307".The hub hosted in a remote machine , am using the IP of the server to connect to the hub.
***Please find the callstack
Traceback (most recent call last):
File "d:\signalrClient.py", line 16, in <module>
connection.start()
File "D:\Python35-32\lib\site-packages\signalr_client-0.0.7-py3.5.egg\signalr\
_connection.py", line 50, in start
File "D:\Python35-32\lib\site-packages\signalr_client-0.0.7-py3.5.egg\signalr\
transports\_auto_transport.py", line 28, in start
File "D:\Python35-32\lib\site-packages\signalr_client-0.0.7-py3.5.egg\signalr\
transports\_ws_transport.py", line 38, in start
File "D:\Python35-32\lib\site-packages\websocket_client-0.40.0-py3.5.egg\webso
cket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "D:\Python35-32\lib\site-packages\websocket_client-0.40.0-py3.5.egg\webso
cket\_core.py", line 214, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "D:\Python35-32\lib\site-packages\websocket_client-0.40.0-py3.5.egg\webso
cket\_handshake.py", line 65, in handshake
status, resp = _get_resp_headers(sock)
File "D:\Python35-32\lib\site-packages\websocket_client-0.40.0-py3.5.egg\webso
cket\_handshake.py", line 122, in _get_resp_headers
raise WebSocketBadStatusException("Handshake status %d", status)
websocket._exceptions.WebSocketBadStatusException: Handshake status 307***
Am not able to find out why this error is hppening and what this 307 status code corresponds to?
Could anyone faced the same issue ?
Please share your insights on the issue.
Thanks in advance
Susmitha

error when using poplib to get email

I am using poplib to get email from the POP3 server.
But this error occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\myutils.py", line 251, in dxDown
m=poplib.POP3('pop3.126.com')
File "C:\Python26\lib\poplib.py", line 83, in __init__
self.sock = socket.create_connection((host, port), timeout)
File "C:\Python26\lib\socket.py", line 500, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
My laptop is in an local network and using a server(ip 192.168.0.1:8080) as proxy to access internet. The error seems poplib cannot interpret the domain "pop3.126.com". How to solve this problem?Thanks!
Your proxy is for http, it doesn't effect the pop3 traffic.
A cursory glance suggests that it's probably not able to resolve the hostname to an IP address.
Can you try one of these:
pop3.126.idns.yeah.net
220.181.15.128
Or paste the output of:
nslookup pop3.126.com

Categories

Resources