关于python urlopen 一个类似radio流的timeout方法
终极解决方法来啦!看代码感受:
import requests
import eventlet
import time eventlet.monkey_patch() try:
with eventlet.Timeout(5):
response = requests.get("http://t.co/iXJWRrfl5n", verify=False) #http://t.co/iXJWRrfl5n is a radio stream!!
#response = requests.get("http://docs.python-requests.org/en/master/user/advanced/", verify=False)
print response.text
except:
print 'error' print response.text
while True:
time.sleep(1)
print 'sec'
from urllib2 import urlopen
from threading import Timer
url = "http://t.co/iXJWRrfl5n"
fh = urlopen(url)
t = Timer(5.0, fh.close)
t.start()
data = fh.read()
t.cancel()
5s以后会把read()方法给掐了
另见
http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
with timeout(seconds=3):
sleep(4)
class timeout:
def __init__(self, seconds=1, error_message='Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
import signal def signal_handler(signum, frame):
raise Exception("Timed out!") signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(10) # Ten seconds
try:
long_function_call()
except Exception, msg:
print "Timed out!" signal.alarm(0) # cancel alarm when function return in time #other code...
http://stackoverflow.com/questions/21965484/timeout-for-python-requests-get-entire-response
import requests
import eventlet
eventlet.monkey_patch() with eventlet.Timeout(10):
requests.get("http://ipv4.download.thinkbroadband.com/1GB.zip", verify=False)
关于python urlopen 一个类似radio流的timeout方法的更多相关文章
- python 创建一个实例:步骤二 添加行为方法,编写方法
添加方法 class Person(): def __init__(self,name,job=None,pay=0): self.name= name self.job = job self.pay ...
- 用python构建一个多维维数组
用python构建一个二维数组 解法? 方法1: num_list=[0]*x//表示位创建一个一维数组为num_lis[x],且数组中的每一项都为0 num_list=[[0]*x for i in ...
- 使用python检测一个设备是否ping的通
使用python检测一个设备是否ping的通 一,subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并 ...
- 【Python】如何基于Python写一个TCP反向连接后门
首发安全客 如何基于Python写一个TCP反向连接后门 https://www.anquanke.com/post/id/92401 0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 制作一个类似苹果VFL的格式化语言来描述UIStackView
在项目中总是希望页面上各处的文字,颜色,字体大小甚至各个视图控件布局都能够在发版之后能够修改以弥补一些前期考虑不周,或者根据统计数据能够随时进行调整,当然是各个版本都能够统一变化.看到这样的要求后,第 ...
- Pyscripter是python下一个非常流行的开源IDE
Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine) 背景 Pyscripter是python下一个非常流行的开源IDE,笔者 ...
- 用 python 写一个年会抽奖小程序
使用 pyinstaller 打包工具常用参数指南 pyinstaller -F demo.py 参数 含义 -F 指定打包后只生成一个exe格式的文件 -D –onedir 创建一个目录,包含exe ...
- 用python 实现一个栈
前言 Python本身已有顺序表(List.Tupple)的实现,所以这里从栈开始. 什么是栈 想象一摞被堆起来的书,这就是栈.这堆书的特点是,最后被堆进去的书,永远在最上面.从这堆书里面取一本书出来 ...
随机推荐
- Java进阶5 面向对象的陷阱
Java进阶5 面向对象的陷阱 20131103 Java是一门纯粹面向对象的编程语言,Java面向对象是基础,而且面向对象的基本语法非常多,非常的细,需要程序员经过长时间的学习才可以掌握.本章重点介 ...
- LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- n阶魔方阵(奇数阵)的输出
需求 要求输出1~n²的自然数构成的魔方阵. STEP 1 什么是魔方阵? 魔方阵,古代又称“纵横图”,是指组成元素为自然数1.2…n2的平方的n×n的方阵,其中每个元素值都不相等,且每行.每列以及主 ...
- Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
感谢:http://blog.chinaunix.net/uid-20681545-id-184633.html提供的解决方案,非常棒 ! 问题说明: 新建一个Spring项目,新建一个Bean类:H ...
- c# 多线程调用窗体上的控件 示例
private delegate void InvokeCallback(string msg); private void SetCountValue(string s) { if (this.fo ...
- AOP代理模式
AOP 在Spring框架中被作为核心组成部分之一,的确Spring将AOP发挥到很强大的功能.最常见的就是事务控制.工作之余,对于使用的工具,不免需要了解其所以然.学习了一下,写了些程序帮助理解. ...
- flowable DmnEngine和DmnEngineConfiguration
一.DmnEngineConfiguration创建实例 DmnEngineConfiguration 提供了7个公共的静态方法,用于创建自身实例. 其中5个是使用spring的机制加载配置文件. 另 ...
- Return type declarations返回类型声明
PHP 7.新增了返回类型声明 http://php.net/manual/en/functions.returning-values.php 在PHP 7.1中新增了返回类型声明为void,以及类型 ...
- 第23课 #error和#line使用分析
#error的用法: 示例程序: #include <stdio.h> #ifndef __cplusplus #error This file should be processed w ...