-2
基本上我複製從計算器這些方法和修改他們爲我的必要性:的Python 3.2:類型錯誤:參數必須是int,或者有一個的fileno()方法
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.
尊敬的領主,請幫我糾正我的計劃!
什麼是'h.sock'失敗時?嘗試打印它和它的'type()'。 – 2014-12-19 04:32:59
我不知道如何打印它。我昨天剛學Python。我嘗試在「if h和select ...」行之前放置print(h.sock)print(h.sock),但是出現此錯誤:AttributeError:'NoneType'對象沒有屬性'sock' – 2014-12-19 04:48:24
你想要做什麼,Python不能自行處理? – 2014-12-19 05:09:44