I am using simple python lib for the SMTP
But i am getting this error:
import smtplib
smtpObj = smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
Using python-2.7
Start a simple SMTP server with Python like so:
python -m smtpd -n -c DebuggingServer localhost:1025
or you can also try gmail smtp setting
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
if you:
python -m smtpd -n -c DebuggingServer localhost:1025
as suggested by Allen Thomas, then make sure you init:
server = smtplib.SMTP(host='localhost', port=1025)
with matching port number, here: 1025.
You need to Enable the Mode of Protocol you are using.Then it will create Bidirectional interaction virtual connection between your device and script.
For Example the following script will require Telnet Mode to be Enabled so that it can call to another Device he is requesting for.
telA = connect(a)
telA.write("xcom Dial Number:43062#10.47.8.108\n")
time.sleep(10)
quit_telnet(telA)
print telA.read_all()
Related
I tried using different ports and find solutions in the internet but I still can't fix it. I tried changing the localhost to 127.0.0.1 and also tried changing the port numbers to all types of values but still won't work. A great help would be greatly appreciated.
import tkinter
import smtplib
import socket
from email.parser import Parser
smtp = smtplib.SMTP
user = ""
password = ""
def connect():
print(msg_entry.get())
smtp = smtplib.SMTP("localhost", 587)
#smtp.login(user,password)
smtp.sendmail(from_entry.get(), to_entry.get(), msg_entry.get())
smtp.quit()
app = tkinter.Tk()
app.title("test")
to_label = tkinter.Label(app,text="To:")
to_entry = tkinter.Entry(app)
from_label = tkinter.Label(app,text="From:")
from_entry = tkinter.Entry(app)
send_button = tkinter.Button(app,text="send",command=connect)
msg_label = tkinter.Label(app,text="Email:")
msg_entry = tkinter.Entry(app,width=50)
#pack(add) the widget to the app.
to_label.pack()
to_entry.pack()
from_label.pack()
from_entry.pack()
msg_label.pack()
msg_entry.pack()
send_button.pack()
#draw the window, have this at the end
app.mainloop()
Whenever I would click the send, I would get the error:
Traceback (most recent call last):
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/ADMIN/PycharmProjects/CourseOutcome3/EmailTransmitter-DAMPAC.py", line 15, in connect
smtp = smtplib.SMTP("localhost", 587)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
You need to have a server on localhost to make it work, i recommend PaperCut, also you may want to check this post of mine about how to create a basic smtp mail server.
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
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.
I tried to start a smtp server with python with the following code:
import smtplib
smtp_server = smtplib.SMTP('localhost')
And I get the following error:
File "test.py", line 2, in <module>
smtp_server = smtplib.SMTP('localhost')
File "/usr/local/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
I tried to telnet localhost 25 and telnet localhost but they both give the following error:
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host: Connection refused
The same python code runs perfectly well on one of my other machines.
Any advice ?
Thanks.
UPDATE:
The machine I'm having problem with is running Python 2.7.2, while the machine that the code works well is running Python 2.6.2. I don't know if this is one of the reasons.
smtplib is SMTP protocol client. What you are looking for is smtpd.
The problem is that smtplib does not provide an SMTP server implementation, it implements SMTP client. Most likely, the code tries to connect to localhost and fails. See http://docs.python.org/2/library/smtplib.html
Is the other machine you write about already running an SMTP server?
And what are you trying to do?
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)