i get the following error when trying a script thaat sends mail
import urllib.request
import re
import smtplib
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
page=urllib.request.urlopen("http://www.crummy.com/")
soup=BeautifulSoup(page)
v=soup.findAll('a',href=re.compile('http://www.crummy.com/2012/07/24/0'))
for link in v:
w=link.get('href')
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( 'xxxxxxxxxxx', 'xxxxxxx' )
server.sendmail( 'xxxxxxxxx', 'xxxxxxxxx', "bonus question is up" )
Traceback (most recent call last): File "C:\Python32\bonus", line 14,
in server = smtplib.SMTP( "smtp.gmail.com", 587 ) File
"C:\Python32\lib\smtplib.py", line 259, in init File
"C:\Python32\lib\smtplib.py", line 319, in connect
self.sock = self._get_socket(host, port, self.timeout) File "C:\Python32 \lib\smtplib.py", line 294, in _get_socket
return socket.create_connection((host, port), timeout) File "C:\Python32\lib\socket.py", line 386, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno 11004] getaddrinfo failed plse advice on the
best way to go round it
The getaddrinfo function has this purpose:
The getaddrinfo function provides protocol-independent translation from an ANSI host name to an address.
If it fails it means that it cannot translate your given hostname to it's respective address. It's essentially doing a DNS query.
Your error number returned "11004" by getaddrinfo has this message associated with it:
Valid name, no data record of requested type. The requested name is
valid and was found in the database, but it does not have the correct
associated data being resolved for. The usual example for this is a
host name-to-address translation attempt (using gethostbyname or
WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX
record is returned but no A record—indicating the host itself exists,
but is not directly reachable.
It seems the name you're looking up has no correct data associated with it.
Are you sure your URL's are correct?
Links:
getaddrinfo: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
WinSock Error Codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
Related
I'm trying to connect to my server using SSH with port 2022 (not 22) in Python. So I wrote the following code that uses Paramiko package:
import sys
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
finally:
client.close()
But when I'm running it in my IDE (PyCharm) I get the following error:
/usr/local/lib/python3.5/dist-packages/paramiko/ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.ecdsa_curve.curve_class(), pointinfo
Traceback (most recent call last):
File "/home/mshapirs/PycharmProjects/OnlineTest.py/OnlineTest.py", line 9, in
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 334, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
You should provide username as a separate parameter, not prepended to the host address.
Look at the docs for .connect. It has username and hostnamelisted separately.
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')
I need to create simple code that will upload a .csv file to an FTP server. The code is below.
import ftplib
import os
import sys
sourceFilePath = '/home/user/dir/'
filename = 'testing.csv'
destinationDirectory = 'anotherDirectory'
server = 'ftp://12.123.12.234'
username = 'aUser'
password = 'passwd1234'
myFTP = ftplib.FTP(server, username, password)
myFTP.cwd('/anotherDirectory/')
myFTP.storbinary('STOR '+filename, open(filename,'rb'))
myFTP.quit()
However, when I run the code, I get the following error:
Traceback (most recent call last):
File "./UploadToFTP.py", line 20, in <module>
File "./UploadToFTP.py", line 13, in uploadFileFTP
myFTP = ftplib.FTP(server, username, password)
File "/usr/lib64/python2.6/ftplib.py", line 119, in __init__
self.connect(host)
File "/usr/lib64/python2.6/ftplib.py", line 134, in connect
self.sock = socket.create_connection((self.host, self.port),
self.timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -3] Temporary failure in name resolution
Has anyone seen this before? It seems rather generic to me and doesn't tell me much. So far as other code I've seen performing this same task, I don't have any currently visible errors. Any help would be appreciated.
The host argument of ftplib.FTP constructor is a hostname/IP address, not a URL.
So this is wrong:
server = 'ftp://12.123.12.234'
It should be:
server = '12.123.12.234'
If your URL contains a custom port, see Python ftplib - specify port.
I'm trying to use a Beaglebone Black (BBB) to send email notifications, but I'm getting caught up on this getaddrinfo error that reads as follows;
socket.gaierror: [Errno -2] Name or service not known
I've been working on this for a while and can't find why this isn't working.
The nano file I"m trying to run:
import smtplib
#import time
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
#time.sleep(1000)
print("SMTP object created...")
smtpObj.ehlo()
#time.sleep(1000)
print("EHLO...")
smtpObj.starttls()
#time.sleep(1000)
print("Starting TLS...")
smtpObj.login('EXAMPLEACCOUNT#gmail.com', 'EXAMPLEPASSWORD')
#time.sleep(1000)
print("Logged into EXAMPLEACCOUNT#gmail.com...")
smtpObj.sendmail('EXAMPLEACCOUNT#gmail.com', 'EXAMPLERECIPIENT', '''Subject:test subject \ntest body
Auto Alert System.''')
{}
#time.sleep(1000)
print("Sending email...")
smtpObj.quit()
#time.sleep(1000)
print("Destorying object.")
The output of invoking the test_email2.py function is as follows:
root#beaglebone:~/Desktop/email_project# python test_email2.py
Traceback (most recent call last):
File "test_email2.py", line 4, in <module>
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
The format I've been following is based on that provided by https://automatetheboringstuff.com/chapter16/
socket.gaierror means that (underlying in libc) getaddrinfo function failed to get IP addresses for domain names you provided. It explains why it failed: [Errno -2] Name or service not known, so it doesn't know about a domain with such a name, smtp.gmail.com. This domain name obviously exists, so you should look into DNS system settings in your BBB system (and it's actually more of a SuperUser community question).
What DNS servers are used in configuration? If you're using a local caching DNS server at loopback, is it up and running? Is it configured properly to allow recursive requests? This particular problem most likely has nothing to do with Python or your code; it's your BBB system cannot resolve at least some, if not all, domain names.
I tried to find some topic related, found some, tried some suggested solution, but seems not to work.
I'm trying to figure out where I did it wrong, I'm basically listing a directory and putting the output into a text file that I want to send via smtp to dest address.
#!/usr/bin/python
import os, sys
import smtplib
from email.mime.text import MIMEText
# GRABE LISTED DIRECTORY AND CREATE THE FILE #
#open folder and list it
Winpath = "C:\"
dirs = os.listdir(Winpath)
#Put it into file
fo = open("activitygraber.txt", "w+")
for file in dirs:
fo.write(file + "\n")
# EMAIL IT TO GIVEN ADDRESS #
msg = MIMEText(fo.read())
fo.close()
msg["Subject"] = "Test ActivityGrab"
msg["From"] = "test#test.me"
msg["To"] = "dest#dest.com"
s = smtplib.SMTP("localhost")
s.sendmail("test#test.me", "dest#dest.com", msg.as_string())
s.quit()
That's simple code mostly from Python documentation (I'm in the learning curve).
The file is being created and ready to send I guess, but somehow the connection is not being created. I guess it's because my smtp port in not open ? should I tried to connect to a mail service ? then send it from there ?
Here the message I get when I try to send it:
Traceback (most recent call last):
File "C:\Users\PC\Desktop\testfile.py", line 33, in <module>
s = smtplib.SMTP("localhost")
File "C:\Python27\lib\smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 310, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 285, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Any help of hint would really be appreciated thanks !
Meanwhile I'm gonna continue to search the cause.
Cheers,
s = smtplib.SMTP("localhost") isn't going to work unless you have a running SMTP server on your local machine (which you probably don't...).
If you do, make sure it's listening on port 25.
If you don't, you will have to use a remote SMTP server. You can use google's if you have a gmail account, or find a free one that is set up as a mail relay (that means you can use it anonymously), but these are very rare these days.