我有一个循环,旨在发出URL请求并读取结果。当请求是"简单"时,代码工作正常。但是,当请求需要更多时间从服务器,然后我的程序永远挂起。它似乎试图同时处理所有请求,但我不明白为什么。我曾尝试研究opener.open(url).read()
的文档,但没有成功找到我的问题的答案。我将非常感谢你的帮助。
这是我的Python 2.7代码:
username = 'the_username'
password = 'the_password'
corpus_name = 'the_corpusname'
root_url = 'root_url'
base_url = '%s/bonito/run.cgi/' % root_url
method = 'view'
attrs = dict(corpname=corp, q='', pagesize=1, format='json',async=0)
# authentication
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({ 'username' : username,
'password' : password,
'submit' : 'ok',
})
data = opener.open('%s/login/' % root_url)
data = opener.open('%s/login/' % root_url, login_data)
pattern = r"[\n\r]"
pattern_re = re.compile(pattern)
with open('input.txt', "r") as f_in, open('output.txt', "a") as f_out:
for line in f_in:
line = pattern_re.sub("",line) # removing \n or \r at the end
query = "[lemma_lc=\"" + line + "\"&pos=\"N\"]"
attrs['q'] = 'q' + query
encoded_attrs = urllib.quote(simplejson.JSONEncoder().encode(attrs))
url = base_url + method + '?json=%s' % encoded_attrs
file1 = opener.open(url)
data = file1.read()
file1.close()
json_obj = simplejson.loads(data)
f_out.write(line + '\t' + str(json_obj.get('concsize', '0')) + '\n')
time.sleep(4)