基本上我从stackoverflow复制了这些方法并根据我的需要对其进行了修改:
from http.client import HTTPConnection, HTTPSConnection
import select
connections = {}
def request(method, url, body=None, headers={}, **kwargs):
scheme, _, host, path = url.split('/', 3)
h = connections.get((scheme, host))
if h and select.select([h.sock], [], [], 0)[0]:
h.close()
h = None
if not h:
Connection = HTTPConnection if scheme == 'http:' else HTTPSConnection
h = connections[(scheme, host)] = Connection(host, **kwargs)
h.request(method, '/' + path, body, headers)
return h.getresponse()
def urlopen(url, data=None, *args, **kwargs):
resp = request('POST' if data else 'GET', url, data, *args, **kwargs)
if resp.status == 400:
fileAccessed = resp.read()
if 'failed to open stream: Permission denied' in str(fileAccessed):
return "permission denied"
else:
return fileAccessed
else:
return "not found"
现在我使用它们与此示例类似:
with open('addons.txt', 'r') as addonsFile:
for line in addonsFile:
addon = line.rstrip()
fileUrl = 'http://www.google.com/%s/ncr' % addon
response = urlopen(fileUrl)
该程序获得第一个插件并使第一个请求正常。在第二次迭代中,我收到此错误:
Traceback (most recent call last):
File "/root/add.py", line 45, in <module>
response = urlopen(fileUrl)
File "/root/add.py", line 26, in urlopen
resp = request('POST' if data else 'GET', url, data, *args, **kwargs)
File "/root/add.py", line 15, in request
if h and select.select([h.sock], [], [], 0)[0]:
TypeError: argument must be an int, or have a fileno() method.
亲爱的stackoverflow领主,请帮我纠正我的程序!
答案 0 :(得分:1)
是啊......所以......我不知道...... 我删除了我收到错误的部分:
if h and select.select([h.sock], [], [], 0)[0]:
h.close()
h = None
if not h:
现在一切正常。