import socket
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysocket.connect(("http://www.py4inf.com",80))
c="http://www.py4inf.com/code/romeo.txt HTTP/1.0\r\n\r\n".encode()
mysocket.send(c)
while True:
data=mysocket.recv(512)
if len(data) <1:
break
else:
print(data.decode())
mysocket.close()
Error:-
Traceback (most recent call last):
File "D:\Python_practice_file\main.py", line 3, in <module>
mysocket.connect(("http://www.py4inf.com",80))
socket.gaierror: [Errno 11001] getaddrinfo failed
I am getting this error while I am learning web socket programming in python but it seems like I am getting an error if anyone can solve this problem I'll be very grateful. I saw some answers but they are not related because i am not using proxy,
Remove the http:// part.
This indicates protocol, which is not part of the FQDN getaddrinfo() tries to resolve.
Also change
c="http://www.py4inf.com/code/romeo.txt HTTP/1.0\r\n\r\n"
to
c="GET /code/romeo.txt HTTP/1.0\r\n\r\n"
But I suggest using urllib or requests libraries if you want to do HTTP requests.
Related
I want to write the Python script to download from S3 bucket using Boto3. My code is working fine when not using proxy, but I have to run this under proxy. I have set proxy in the Boto3.client(config=) and I get the following error.
Proxy set as:
s3 = boto3.client('s3',
aws_access_key_id = usrkey,
aws_secret_access_key = sctkey,
region_name = 'eu-west-2',
config=Config(proxies={'http': '<proxy-server>:<port>'})
)
still end up with the error
Traceback (most recent call last):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
and at the end:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:"<URL>".
I can connect using WinSCP to AWS-S3, with proxy, that is working fine. But it pops up a warning message that I have to accept every time, by clicking ok. What is the problem here, I don't understand with Python.
Make sure your <proxy-server> is a host name:
config=Config(proxies={'http': 'example.com:port'}
Not a URL:
config=Config(proxies={'http': 'http://example.com:port'}
I get this error:
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 286, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
When I enter wrong hostname. I want to put an exception, giving message that wrong hostname was entered, but Python does not recognize that error and I get global name gaierror not defined.
I was trying like this:
try:
ssh.connect(rec.host, username=rec.user, password=rec.password)
except gaierror:
print 'blablabla'
Then it gives this error:
except gaierror:
NameError: global name 'gaierror' is not defined
Do I need to define that error myself somehow or I need to call something from paramiko, so python would understand that exception?
Import the gaierror exception from the socket module. Docs
import socket
try:
# Your code.
except socket.gaierror:
# Handle exception.
Name resolution may fail because there is no ip associated with the hostname, or because the DNS server cannot be reached. Unfortunately, Python's socket.create_connection and socket.gethostbyname functions seem to raise the same error in both situations:
$ python3 -c 'import socket; socket.create_connection(("www.google.com_bar", 80))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
$ python3 -c 'import socket; socket.gethostbyname("www.google_bar.com")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno -5] No address associated with hostname
$ sudo vim /etc/resolv.conf # point to non-existing nameserver
$ python3 -c 'import socket; socket.create_connection(("www.google.com", 80))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
$ python3 -c 'import socket; socket.gethostbyname("www.google.com")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno -5] No address associated with hostname
Is there any way to distinguish these two cases that does not require me to perform a second lookup for a "known-good" hostname?
The solution should work under Linux.
You can use the dnslib library client to make the DNS request yourself. The client provides dig like functionality that can indicate if an address fails to resolve (NXDOMAIN) compared to just failing to resolve (which unfortunately just blocks - see patch below).
You use it like so:
from dnslib import DNSRecord, RCODE
# I have dnsmasq running locally, so I can make requests to localhost.
# You need to find the address of the DNS server.
# The /etc/resolv.conf file is quite easily parsed, so you can just do that.
DNS_SERVER = "127.0.0.1"
query = DNSRecord.question("google.com")
response = DNSRecord.parse(query.send(DNS_SERVER, 53, False))
print RCODE[response.header.rcode] # prints 'NOERROR'
query = DNSRecord.question("google.com_bar")
response = DNSRecord.parse(query.send(DNS_SERVER, 53, False))
print RCODE[response.header.rcode] # prints 'NXDOMAIN'
# To avoid making the DNS request again when using the socket
# you can get the resolved IP address from the response.
The problem comes when making a connection to a non existant DNS Server. Every time I have tried this the request just hangs. (When I make the same requests on the command line, using something like netcat, the request also just hangs. I may be picking random IPs poorly and suffering from firewalls that just drop the packets)
Anyway you can alter the source code to add a timeout. You can view the relevant method in the source here (also mirrored on github). What I changed was:
--- a/dns.py
+++ b/dns.py
## -357,6 +357,7 ##
response = response[2:]
else:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
+ sock.settimeout(10)
sock.sendto(self.pack(),(dest,port))
response,server = sock.recvfrom(8192)
sock.close()
After doing this the DNS request timed out.
If this fails:
ssh = paramiko.SSHClient()
ssh.connect( host, username = USER , pkey = MY_KEY, timeout = 2)
I get a traceback like:
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in bs_process
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 282, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
I cant figure what kind/kinds of errors Paramiko throws for bad connect attempts. Which are the exception classes and how can I import them?
You can start by looking at the API documentation, for all classes ending in Exception:
http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.connect
Then, you should also catch socket.error. I think that will get you pretty much everything. socket.gaierror is a subclass of socket.error, for example.
The accepted answer has a broken link. The documentation for Paramiko now lives at:
http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.connect
It the "connect" method will raise the following:
BadHostKeyException – if the server’s host key could not be verified
AuthenticationException – if authentication failed
SSHException – if there was any other error connecting or establishing an SSH session
socket.error – if a socket error occurred while connecting
The problem is with the call to ssh.connect().
In this case, is necessarily specified the connection port.
Example:
ssh.connect(server, port=22, username=user, pkey=key)
That work for me.
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