code runs indefinitely while trying to send mail via python - python-2.7

I am trying to send mail using python via my own setup mail server
import smtplib
SERVER='myserverdomain.com'
server = smtplib.SMTP(SERVER,587)
server.starttls()
server.login(USER,PASS)
server.sendmail(FROM, TO, message)
server.quit()
But when I run the above code after line 3 it is not executing.Kind of like goes into an infinite loop.Why could this be
I am able to ping my server successfully.

import smtplib
SERVER='myserverdomain.com'
server = smtplib.SMTP_SSL(SERVER,587)
server.login(USER,PASS)
server.sendmail(FROM, TO, message)
server.quit()
This answer worked

Related

sending mail with the use of asterisk voicemail

I am using asterisk and via extensions.conf I have to send voicemail to mail by using python script.
Python script is running fine but I don't have idea how to use that with extensions.
SMTP code is working fine.
context are below-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "from"
toaddr = "to"
As per the comments in your questions, seems you already have it working with Asterisk built-in, so you should have a valid reason to process outside, being this the case, maybe you could use Asterisk System application to call the script from the dialplan or externnotify in voicemail.conf to call the Python script which will receive (need to test) as parameters: context, extension, new voicemails, old voicemails, urgent voicemails.

"BrokenPipeError" with Flask and requests-toolbelt

I am trying to understand more precisely how Http connections work with Flask, so I tried writing a very simple app and another simple connection with requests and requests-toolbelt :
app = Flask('file-streamer')
#app.route("/uploadDumb", methods=["POST"])
def upload_dumb():
print("Hello")
return Response(status=200)
So basically this server should just receive a request and return a response.
Then I implemented a simple piece of code that sends requests with toolbelt :
import requests
from requests_toolbelt.multipart import encoder
values = {"file": ("test.zip", open("test.zip", "rb"), "application/zip"), "test": "hello"}
m = encoder.MultipartEncoder(fields=values)
r = requests.post(url="http://localhost:5000/uploadDumb", data=m, headers={"Content-Type": m.content_type})
The file I'm sending is a pretty large file that I want to upload with streaming.
The thing is, I expected the Flask server to wait for the whole file to be sent (even if the file is useless), then return a response, but that's not what's happening.
Actually, Flask responds at the very beginning of the sending process, returns a 200 response, which causes the 'requests' side to end with a "BrokenPipeError".
Could someone explain to me what is happening there ?

send SMTP email for office365 with python using tls/ssl

i'm trying to create a python script that sends emails with some messages.
my setup is based on a smtp.office365.com. i created some script to do the job.
#!/usr/bin/python
print ("HI! let's start")
import smtplib
import getpass
port=587
smtp_server="smtp.office365.com"
sender_email='mymail#something.com'
receiver_email='mymail#something.com'
print ("defining server")
server = smtplib.SMTP(smtp_server,port)
password=getpass.getpass("enter password")
message='''
Hi,
THIS IS A TEST MESSAGE.
'''
print ("logging in")
server.login(sender_email,password)
print ("sending mail now")
server_sendmail(sender_email,receiver_email,message)
print ("all must be done by now")
But due to some reasons, it gets stuck after printing "defining server". and never reaches to asking for password. apparently the smtplib.SMTP command hangs. can you tell why this happens and how to get rid of it? my setup is as following :
Server: smtp.office365.com
/sjmail.com
security : STARTTLS
Port : 587
Vishesh Arya!
SMTP AUTH extension not supported by Office 365 account. Search for Microsoft Graph to do this.

unable to post data to mosquitto broker continuously

I am trying to send data continuously from raspberry pi to a windows pc using MQTT,
I am trying to send 5 data to mosquitto, but the mosquitto seems to get only one value
coding in raspberry pi
import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
data={"protocol":"mqtt"}
client.publish("/test",str(data))
coding at the broker to receive data is
import paho.mqtt.client as mqtt
print("attempting to connect...")
def on_connect(client, userdata, flags, rc):
if(rc==0):
print("connection successful broker linked")
elif(rc==1):
print("connection refused - incorrect protocol version")
elif(rc==2):
print("connection refused - invalid client identifier")
elif(rc==3):
print("connection refused- server unavailable")
elif(rc==4):
print("connection refused- bad username or password")
elif(rc==5):
print("connection refused- not authorised")
else:
print("currently unused")
client.subscribe("s/test")
def on_message(client, userdata, msg):
data=eval(msg.payload)
print(data)
client = mqtt.Client()
client.connect("localhost",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
Have you thought about following the answer I posted here?
https://github.com/eclipse/mosquitto/issues/972
You need to make sure the network loop runs for the publishing client as well a the subscriber. The network loop actually handles sending the messages.
The following is the simplest modification to your code.
import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
data={"protocol":"mqtt"}
client.publish("/test",str(data))
client.loop()

fetch website with python include j.s css

i'm trying to fetch a whole website include the JavaScript and css file while using python.
The script get a "GET" request and send back the website (local proxy).
here is my code :
class myHandler(BaseHTTPRequestHandler):
# Handler for the GET requests
def do_GET(self):
opener = urllib.FancyURLopener({})
f = opener.open("http://www.ynet.co.il")
self.wfile.write(f.read())
return
try:
# Create a web server and define the handler to manage the
# incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ', PORT_NUMBER
# Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
The result for this code is only the html is present to the client.
Thanks a lot for the help, i'm Trying to solve that for few days with no result any .