2013-11-23 90 views
3

我在OSX Mavericks上运行并安装了python 3.3.3。python - 代码在Linux上正常工作,但在OSX上崩溃

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin) 

我试图运行这个(未完)代码(一个简单的HTTPS GET和POST与饼干),但它有一个未知的错误崩溃:

import urllib.request as rqst 
import re 
import http.cookiejar 

def LoginICorsi(): 
     loginUrl = "https://www2.icorsi.ch/auth/shibboleth/" 
     rqst.install_opener(rqst.build_opener(rqst.HTTPCookieProcessor(http.cookiejar.CookieJar()))) 
     pageBytes = rqst.urlopen(loginUrl) 

     pageString = pageBytes.read().decode("utf-8") 

     action = re.findall(r'action="([^"]*)', pageString)[0] 

     loginPostUrl = "https://wayf.switch.ch" + action 
     loginPostUrl = loginPostUrl.replace("&", "&") 

     print("Posting USI to "+loginPostUrl) 
     postDATA = "user_idp=https://login2.usi.ch/idp/shibboleth".encode("utf-8") 
     usiLoginRequest = rqst.Request(loginPostUrl) 
     usiLoginRequest.add_header("Content-Type", "application/x-www-form-urlencoded") 
     usiLoginUrl = rqst.urlopen(usiLoginRequest, data=postDATA) 
     usiLoginResult = usiLoginUrl.read().decode("utf-8") 
     print(usiLoginResult) 

的问题是,这种代码在Ubuntu上工作

Python 3.3.3 (default, Nov 20 2013, 00:22:18) 
[GCC 4.8.2] on linux 

所以我想代码是正确的。

此外,当我使用Fiddler将HTTPS的系统代理设置为代理时,代码在我的Mac上工作。

这是追溯pastebin

我错过了什么?这是否与OSX,Python,HTTPS服务器或只是我的Mac? 与服务器握手时会出现问题,但我不明白它为什么适用于Linux。

+1

你是如何在Mac上安装Python 3.3.3的?一个二进制安装程序(如果是的话,来自python.org或其他)或源代码构建(如果是,Homebrew,MacPorts或手动)?这看起来像是'ssl'模块的一个问题,如果你使用围绕OS X 10.8的OpenSSL而设计的配方,但是实际上拥有OS X 10.9的机器,这很容易得到。 – abarnert

+0

我下载了二进制安装程序,http://www.python.org/ftp/python/3.3.3/python-3.3.3-macosx10.6.dmg –

回答

0

强烈建议您从urllibs迁移到python requests模块。

说真的,值得5分钟easy_install/pip并移植你的代码。它将处理大多数SSL问题,使其更加优雅,而且不那么痛苦。

+0

我正在考虑使用外部模块,但我会真的更喜欢留在Python本地模块... 感谢您的答复,我会尝试与python的请求,让你知道。 –

+0

我知道想要保留在std lib中,但是请求是值得的。认真。 – rdodev

+0

有许多事情要求更好(包括OP在硬盘上做的一些事情,比如cookies)。但基本的SSL协商不是其中之一。它使用'ssl'模块,就像'urllib'一样。 – abarnert

相关问题