socket ResourceWarning using urllib in Python 3

I am using a urllib.request.urlopen() to GET from a web service I'm trying to test. This returns an HTTPResponse object, which I then read() to get the response body. But I always see a ResourceWarning about an unclosed socket from socket.py Here's the relevant function: from urllib.request import Request, urlopen def get_from_webservice(url): """ GET from the webservice """

使用Python 3中的urllib实现套接字ResourceWarning

我正在使用urllib.request.urlopen()从我试图测试的Web服务中获取GET。 这将返回一个HTTPResponse对象,然后我读取()以获取响应正文。 但是我总是从socket.py中看到关于一个未关闭套接字的ResourceWarning 这是相关的功能: from urllib.request import Request, urlopen def get_from_webservice(url): """ GET from the webservice """ req = Request(url, method="GET", headers=HEADERS) with urlop

For a Python module, what are the standard global variables to declare?

In reading other Python modules, I've seen that many people often include __version__ and __author__ global variables in their source files (its even mentioned in PEP3001). I'd like to document my code with a reasonable set of these variables. What is a list of global variables that might be commonly included? There isn't a specific standard for those global variables - as noted i

对于Python模块,要声明哪些标准全局变量?

在阅读其他Python模块时,我已经看到很多人经常在他们的源文件中包含__version__和__author__全局变量(甚至在PEP3001中提到过)。 我想用一组合理的变量来记录我的代码。 什么是可能通常包含的全局变量列表? 对于这些全局变量没有具体的标准 - 正如您所链接的PEP所指出的那样,他们试图达到一个标准,但并没有以任何单一形式被普遍接受。 真正的标准是PyPI元数据,它使用distutils (或兼容接口)在您的模块的setup.py文

Block mean of numpy 2D array

I want to find block mean of a 2D array in NumPy. For simplicity, let us assume that the array is as follows: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) I want to divide this array into 3 blocks of size 2x4, and then find the mean of all three blocks (so that the shape of the mean is 2x4. The first block is formed by the f

块意味着numpy二维数组

我想在NumPy中查找二维数组的块平均值。 为了简单起见,让我们假设数组如下所示: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) 我想把这个数组分成3个大小为2x4的块,然后找出所有三个块的平均值(这样平均值的形状就是2x4。第一个块由前4列组成,下一个由下一个块组成4栏等等,所以我的块是: array([[0, 1, 2, 3], [12, 13, 14, 15]]) a

How to get POST variables in Python, when using gevent?

This is the question:How to get POST variables in Python, when using gevent? The following is passed to the application: def application(env, start_response): And this is the other part: if __name__ == '__main__': print 'Serving on 8080...' WSGIServer(('', 8080), application).serve_forever() But env doesn't contain my POST! Please enlighten me - where does my misunderstanding lie? T

如何在使用gevent时在Python中获取POST变量?

这是一个问题:如何在使用gevent时在Python中获取POST变量? 以下内容被传递给应用程序: def application(env, start_response): 这是另一部分: if __name__ == '__main__': print 'Serving on 8080...' WSGIServer(('', 8080), application).serve_forever() 但是env不包含我的POST! 请赐教 - 我的误解在哪里? 谢谢! 你需要解析请求体environ['wsgi.input'].read() 。 但是,最好使用Web框架为您做

How can I cut down the number of queries?

This code is currently executing about 50 SQL queries: c = Category.objects.all() categories_w_rand_books = [] for category in c: r = Book.objects.filter(author__category=category).order_by('?')[:5] categories_w_rand_books.append((category, r)) I need to cut down the number of used queries to the minimum to speed up things and do not cause server load. Basically, I have three model

我怎样才能减少查询次数?

此代码当前正在执行大约50个SQL查询: c = Category.objects.all() categories_w_rand_books = [] for category in c: r = Book.objects.filter(author__category=category).order_by('?')[:5] categories_w_rand_books.append((category, r)) 我需要将使用的查询数量降至最低,以加快速度并且不会导致服务器负载。 基本上,我有三种模式:类别,作者,书。 作者属于类别(不是书籍),我需要获得所有类别的列表

python ball physics simulation

I have seen the great tutorial by Peter Colling Ridge on http://www.petercollingridge.co.uk/pygame-physics-simulation/ and I am extending the PyParticles script The code is available on the site(for free), I am using PyParticles4.py Classes used in the tutorial The Particle Class Circular 2d objects with radius,mass,velocity,location The Spring Class A spring that binds 2 objects

python球物理模拟

我见过Peter Colling Ridge的精彩教程 http://www.petercollingridge.co.uk/pygame-physics-simulation/ 我正在扩展PyParticles脚本 代码在网站上可用(免费),我使用PyParticles4.py 本教程中使用的类 粒子类 圆形2d物体,半径,质量,速度,位置 春季班 一个弹簧绑定两个对象(粒子)并使用胡克定律 (F = -kx)来确定它们之间的相互作用 环境类 粒子相互作用的环境 我想知道是否可以使用2粒子并制作一

Manage token life duration with SimpleCookie in Python

Already got token working correctly, which is set this way: session_cookie = SimpleCookie() session_cookie['key'] = any_string_value session_cookie['key']["Path"] = '/' headers = [] headers.extend(("set-cookie", morsel.OutputString()) for morsel in session_cookie.values()) start_response(status, headers) I am also able to read the token and extract the information I need: # Get cooki

使用Python中的SimpleCookie管理令牌使用期限

已经得到令牌正确工作,这是这样设置的: session_cookie = SimpleCookie() session_cookie['key'] = any_string_value session_cookie['key']["Path"] = '/' headers = [] headers.extend(("set-cookie", morsel.OutputString()) for morsel in session_cookie.values()) start_response(status, headers) 我也能够读取令牌并提取我需要的信息: # Get cookies cookies = request.get_cookies() # Get current

SCons to generate variable number of targets

I am trying to get SCons to generate multiple targets (number unknown directly in SConscript ). I have directory like: headers/ Header1.h Header2.h Header3.h Header4.h meta/ headers_list.txt Now I want SConscript to read headers_list.txt , basing on its contents pick files from headers/ directory (ie it might contain only Header1 and Header3 ), for each of those I want to generate s

SCons生成可变数量的目标

我试图让SCons生成多个目标(数字直接在SConscript未知)。 我有如下目录: headers/ Header1.h Header2.h Header3.h Header4.h meta/ headers_list.txt 现在我想让SConscript读取headers_list.txt ,根据它的内容从头headers/目录中选择文件(即它可能只包含Header1和Header3 ),对于我想要使用某个函数生成源文件的每个文件。 我一直在尝试使用env.Command来做到这一点,但问题是它需要调用者指定目标列表,

Pydev and *.pyc Files

I am using Eclipse 4.2.1 with the pydev Plugin (version 2.7.1) for python development. It seems that pydev has problems with precompiled python files (*.pyc files). I am using a package which is only provided with such precompiled files. When importing a module, pydev shows "unresolved import" error. And also tool tips are not provided for such precompiled modules. But the code c

Pydev和* .pyc文件

我正在使用Eclipse 4.2.1和pydev插件(版本2.7.1)进行python开发。 看来pydev在预编译的python文件(* .pyc文件)方面存在问题。 我正在使用仅包含这些预编译文件的包。 导入模块时,pydev显示“未解析的导入”错误。 此外,这些预编译模块也不提供工具提示。 但是代码可以没有任何错误地执行。 有什么方法可以“教”pydev,如何处理* .pyc文件? 这看起来像一个错误。 http://sourceforge.net/projects/pydev/forums/f

python compare datetimes with different timezones

I'm implementing feature with scheduled publishing of object. User chooses the time to publish and i created a cron task to run every minute and check if it's the time to publish. Users are from different timezones. So i need to compare two datetimes: >>user_chosen_time datetime.datetime(2012, 12, 4, 14, 0, tzinfo=tzinfo(120)) >>curdate=datetime.datetime.now() datetime.d

python比较日期时间与不同的时区

我正在实现计划发布对象的功能。 用户选择发布时间,我创建了一个cron任务来运行每分钟,并检查是否是发布时间。 用户来自不同的时区。 所以我需要比较两个日期时间: >>user_chosen_time datetime.datetime(2012, 12, 4, 14, 0, tzinfo=tzinfo(120)) >>curdate=datetime.datetime.now() datetime.datetime(2012, 12, 4, 18, 4, 20, 17340) >>user_chosen_time==curdate *** TypeError: can't compare of