Connecting to Gmail from Python - python

I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host

What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.

Related

PostgreSQL OSError: Multiple exceptions: [Errno 111] Connect call failed ('127.0.0.1', 5432), [Errno 99] Cannot assign requested address

So I'm new to PostgreSQL and I made my server and database and the table and stuff then I do back to repl.it discord.py and I tried running this:
async def create_db_pool():
client.pg_con = await asyncpg.create_pool(database= "mainbank", user = "postgres", password = password)
client.loop.run_until_complete(create_db_pool())
Error:
Traceback (most recent call last):
File "main.py", line 204, in <module>
client.loop.run_until_complete(create_db_pool())
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "main.py", line 39, in create_db_pool
client.pg_con = await asyncpg.create_pool(database= "mainbank", user = "postgres", password = password)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/pool.py", line 407, in _async__init__
await self._initialize()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/pool.py", line 435, in _initialize
await first_ch.connect()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/pool.py", line 127, in connect
self._con = await self._pool._get_new_connection()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/pool.py", line 477, in _get_new_connection
con = await connection.connect(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connection.py", line 1980, in connect
return await connect_utils._connect(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 677, in _connect
raise last_error
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 661, in _connect
con = await _connect_addr(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 634, in _connect_addr
tr, pr = await compat.wait_for(connector, timeout=timeout)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/compat.py", line 103, in wait_for
return await asyncio.wait_for(fut, timeout)
File "/usr/lib/python3.8/asyncio/tasks.py", line 494, in wait_for
return fut.result()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 544, in _create_ssl_connection
tr, pr = await loop.create_connection(
File "/usr/lib/python3.8/asyncio/base_events.py", line 1033, in create_connection
raise OSError('Multiple exceptions: {}'.format(
OSError: Multiple exceptions: [Errno 111] Connect call failed ('127.0.0.1', 5432), [Errno 99] Cannot assign requested address
What is wrong? It is my side or is it PostgreSQL? I have the Host as localhost and the port as 5432 (all default)
It looks like you have too many TCP connections on that machine. Whenever you connect(2) to establish a TCP connections (and you didn't bind(2) the socket to a port), the network socket is assigned an ephemeral port by the operating system. If there is no free ephemeral port on the machine, you get the error EADDRNOTAVAIL:
EADDRNOTAVAIL
(Internet domain sockets) The socket referred to by sockfd had not previously been bound to an address and, upon attempting to
bind it to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. See the
discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7).
Either open fewer TCP connections or increase the range for ephemeral ports.
I had the same exact error running a Python FastAPI app in a Docker container trying to connect to another container with http3.AsyncClient (looks in both cases asyncio library is used) to GET a response from another app. The answer above by #Laurenz Albe directed me to
docker system prune -a
The app ran after that. I also can reproduce this error if trying to connect to 127.0.0.1 but container not on the host network.

Simple telnetlib.Telnet not working. Connection Refused Error

I have a simple script that is failing. I am running the service and the firewall is off. Anyone have any ideas on what's wrong? I can't netcat to it either with cmd : nc -vz 127.0.0.1 40400
Python 2.7
import telnetlib
tn = telnetlib.Telnet("127.0.0.1", 40400)
Traceback (most recent call last): File
"/Users/mhegglin/assignments/telemetry/Basis.py", line 3, in
tn = telnetlib.Telnet("127.0.0.1", 40400)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py",
line 211, in init
self.open(host, port, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py",
line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 575, in create_connection
raise err socket.error: [Errno 61] Connection refused
My telnet server wasn't staying online so when I tried to telnet it was failing

Python FTP (from ftplib module) fails on login to SFTP server

I am doing the following
>> from ftplib import FTP
>> s = FTP('host','user','password') # Connect
and it fails giving the following
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 117, in __init__
self.connect(host)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err socket.error: [Errno 60] Operation timed out
I know that host, user, passwd are correct
How do I debug/fix this error?
WinSCP (which you've otherwise been using to connect to the same server) supports SFTP and SCP, not FTP.
To write a Python program using SFTP, you should be using the Paramiko library.
Try doing it like this:
try:
s = FTP(host)
s.login(user, password)
except Exception, e:
print "The error was:", str(e)

how do i send an email in python?

i've looked online but every time i try to connect to the localhost it says connection refused. Do i need to sign into a valid email address to send email?
Here is my EXACT code.
>>> import smtplib
>>> sender = 'from#fromdomain.com'
#this is my exact sender name bc i don't know if i need to use a valid email address or if i can just make up one since i dont need a password and username
>>> receiver = ['to#todomain.com']
#again, i dont know what to use for the receiver email address
>>> message = 'this is a test'
>>> s = smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
s = smtplib.SMTP('localhost')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 61] Connection refused
the connection refused error is my problem. i've looked online but i can't figure out how to connect it.
If you want to use gmail to send your message there is some code at: http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/ that you can use. It should be pretty self explaining..
Do you have a smtp server running in your computer? Localhost refers to your own computer.

how to send email in django? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Django - [Errno 111] Connection refused
I have found a a link on how to send email with django : https://docs.djangoproject.com/en/dev/topics/email/
i followed the example and i got this:
>>> from django.core.mail import send_mail
>>> send_mail('dfdf', 'dfdfdf', 'logadrinab#agile.com.ph', ['ddcahanap#agile.com.ph'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.7/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused
>>>
i think it's because of EMAIL_HOST and EMAIL_PORT, if it is so
where can I find it and how to use it? If it is not can anyone give me an idea on how to send email in django?
thanks in advance...
In your settings.py for your project, make sure you set your EMAIL_HOST, EMAIL_PORT and other settings (like a username or password if your host requires authentication).
Also, make sure you are running the console using the manage.py shell command, and not your normal python shell, so django loads the settings correctly.
You are getting an error because you have no mail transfer agent installed on you system.
You just need to install postfix package in default configuration (it will ask you some questions during install, just choose defaults).
And there is no need to altering your configuration or settings.py or whatever.
You'll need to correctly configure the SMTP email backend for your current host and test it interactively like you just did.
It's possible that they're using TLS or an alternate port--you should look it up in your account settings. Alternatively you can configure Django to use a remote service host, like Gmail, to get yourself started if you have no other options.
You are right, you need to specify correct host and port in settings.py.
What server you are going to use? If it is your hosting provider's server, for example, it is better to contact hosting support to get this info.

Categories