我使用virtualenv并在我的项目libs文件夹中复制了site-packages文件夹的内容。
这是我的代码;
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), "libs"))
import webapp2
from bs4 import BeautifulSoup
from ntlm import HTTPNtlmAuthHandler
import urllib2
import mechanize
class MainHandler(webapp2.RequestHandler):
def get(self):
usr = self.request.get('usr')
password = self.request.get('pw')
url = "http://www.dogus.edu.tr/dusor/"
user = 'ata.dogus.edu.tr\\' + usr
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
br = mechanize.Browser()
br.handlers = [handler for handler in br.handlers if not isinstance(handler, (mechanize._http.HTTPRobotRulesProcessor))]
br.add_handler(auth_NTLM)
try:
br.addheaders = [('Cookie', br.open(url).info().dict['set-cookie'].split(';')[0])]
except KeyError:
self.response.write('Hatali Sifre veya kullanici adi.')
return
response = br.open(url+'FrmMain.aspx')
response = br.open(url+'FrmNot.aspx')
rows = [row.contents for row in BeautifulSoup(response.read()).find_all('table', id='dgnot')[-1].find_all('tr', align='left')]
dersler = [[i.string.strip() for i in row] for row in rows]
resp = '\n<br><br>\n'.join([' - '.join(ders) for ders in dersler])
print resp
self.response.write(resp)
self.response.write('AAAAA')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host='127.0.0.1', port='8080')
if __name__ == '__main__':
main()
从virtualenv运行此脚本时,python http://127.0.0.1:8080/?usr=1331025&pw=123456
会返回预期结果。但是,将我的应用程序上传到GAE后,我收到这样的错误;
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/main.py", line 27, in get
br.addheaders = [('Cookie', br.open(url).info().dict['set-cookie'].split(';')[0])]
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_mechanize.py", line 203, in open
return self._mech_open(url, data, timeout=timeout)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_mechanize.py", line 230, in _mech_open
response = UserAgentBase.open(self, request, data)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_opener.py", line 204, in open
response = meth(req, response)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_urllib2_fork.py", line 457, in http_response
'http', request, response, code, msg, hdrs)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_opener.py", line 221, in error
result = apply(self._call_chain, args)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/mechanize/_urllib2_fork.py", line 332, in _call_chain
result = func(*args)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/ntlm/HTTPNtlmAuthHandler.py", line 99, in http_error_401
return self.http_error_authentication_required('www-authenticate', req, fp, headers)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/ntlm/HTTPNtlmAuthHandler.py", line 35, in http_error_authentication_required
return self.retry_using_http_NTLM_auth(req, auth_header_field, None, headers)
File "/base/data/home/apps/s~dusorbb/1.375812550365780990/libs/ntlm/HTTPNtlmAuthHandler.py", line 63, in retry_using_http_NTLM_auth
r._safe_read(int(r.getheader('content-length')))
AttributeError: HTTPResponse instance has no attribute '_safe_read'
此外,我尝试将库复制到应用程序文件夹的根目录并注释掉行sys.path.append(os.path.join(os.path.dirname(__file__), "libs"))
并得到相同的错误。这可能是什么问题?