编译后的 Twilio Python 模块错误 [英] Twilio Python Module Errors After Compiling

查看:24
本文介绍了编译后的 Twilio Python 模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的程序,可以打开一个 csv 文件并将其中的所有数字输入文本.我使用 Twilio (twilio-python) 作为服务提供商.我的代码作为 python 脚本运行良好.但是,当我编译脚本(使用 py2exe)时,exe 文件出错.这是我从日志文件中收到的错误....

I have written a simple program that opens a csv file and text all the numbers in it. I am using Twilio (twilio-python) as a service provider. My code works fine as a python script. However, when I compile the script (using py2exe), the exe file errors. This is the error I receive from the log file....

 Traceback (most recent call last):
 File "sms.py", line 39, in <module>
 File "twilio\rest\resources\messages.pyc", line 112, in create
 File "twilio\rest\resources\base.pyc", line 352, in create_instance
 File "twilio\rest\resources\base.pyc", line 204, in request
 File "twilio\rest\resources\base.pyc", line 129, in make_twilio_request
 File "twilio\rest\resources\base.pyc", line 101, in make_request
 File "httplib2\__init__.pyc", line 1570, in request
 File "httplib2\__init__.pyc", line 1317, in _request
 File "httplib2\__init__.pyc", line 1252, in _conn_request
 File "httplib2\__init__.pyc", line 1021, in connect
 File "httplib2\__init__.pyc", line 80, in _ssl_wrap_socket
 File "ssl.pyc", line 387, in wrap_socket
 File "ssl.pyc", line 141, in __init__
 ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate               
 routines:X509_load_cert_crl_file:system lib

当我使用未编译的代码(如下)时,我没有收到此错误

I don't recieve this error when i am using the un-compiled code (below)

  import sys #2 params --- /path/to/contact/file    --- up to 160 char msg
  import csv
  import time

  from twilio.rest import TwilioRestClient 
  ACCOUNT_SID = "**************************" 
  AUTH_TOKEN  = "**************************" 
  client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) 

  sys.argv.pop(0)
  contactFile = sys.argv[0]
  sys.argv.pop(0)
  msg = (' ').join(sys.argv)

  print contactFile
  print " "
  print msg

  info = []
  with open(contactFile,'rb') as csvfile:
   reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in reader:
        info.append(row)

  contactCount = len(info)-1

  if contactCount > 0:
     #remove first item from list because its not a value that is needed....
     info.pop(0)

   for i in info:
       print " "
       contactName = i[0]
       phoneNumber = i[1]
       print "Texting " + contactName + "... \n"
       client.messages.create(
       to=phoneNumber, 
       from_="+14782856136",
       body=msg   
       )
       time.sleep(1.5)
    else:
     print("SMSify Error \n The contact file doesn't have any contacts in it.")

对正在发生的事情有什么想法吗??

Any thoughts on what is going on??

这是我的 setup.py 文件

Here is my setup.py file

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
Mydata_files = [('cacert.pem', ['C:\\Python27\\Lib\\site-      
packages\\twilio\\conf\\cacert.pem'])]

setup(
   console=['sms.py'],
   data_files = Mydata_files,
   options={
              "py2exe":{
                    "bundle_files": 1,
                    "compressed": True
                }
         }
 )

推荐答案

这是因为 self-signed certificate 文件在包中丢失.

It's happened because self-signed certificate file missed in bundle.

这个问题对于 requestshttplib2 模块是一样的.

This problem is same for requests and httplib2 modules.

例如,如果您有一个名为 req_example.py 的文件,它使用 request 模块:

For example, if you have a file named req_example.py that using request module:

import requests
url = 'https://google.com/'
requests.get(url)

当您将它作为 python req_example.py 运行时它可以工作,但是当捆绑它时,它不起作用.

It works when you run it as python req_example.py, but when bundle it, it's not working.

或者如果你有一个名为 http2_example.py 的文件,它使用了 http2 模块:

Or if you have a file named http2_example.py that using http2 module:

import httplib2
url = 'https://google.com/'
http = httplib2.Http()
http.request(url)

当您将其作为 python http2_example.py 运行时它可以工作,但是当捆绑它时,它不起作用.

It works when you run it as python http2_example.py , but when bundle it, it's not working.

要解决这个问题,您有两种选择,一种是bad,一种是good.

To fix that, you have two options, one bad and one good.

  1. 禁用验证 SSL 证书:

  1. Disable verifying SSL certificates:

要为 requests 模块做到这一点:

To do that for requests module:

import requests
url = 'https://google.com/'
requests.get(url, verify=False)

对于 httplib2 模块:

import httplib2
http = httplib2.Http(disable_ssl_certificate_validation=True)
http.request(url)

  • 自签名证书文件添加到捆绑包中:

    对于 requests 模块,文件 cacert.pem 位于:

    For requests module, the file cacert.pem is located in:

    .../PythonXX/lib/site-packages/requests/cacert.pem

    对于 httplib2 模块在:

    .../PythonXX/lib/site-packages/httplib2/cacerts.txt

    对于他们中的每一个,您都可以将其复制到您的项目内部(或仅对其进行寻址),

    For each of them, you can copy it to inside of your project (or just address to it),

    并配置 setup.py 以包含它:

    setup(console=['temp.py'],
        # for `requests` module
        data_files=['cacert.pem'] ) # or data_files=['cacerts.txt'] ) for `httplib2`
    

    并将您的代码更改为使用它,对于 request 模块:

    And change your code to using that, for request module:

    import os 
    import requests 
    url = 'https://google.com/' 
    cert ='cacert.pem'
    # or os.environ['REQUESTS_CA_BUNDLE'] = cert 
    os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.getcwd(), cert)
    requests.get(url)
    

    对于 httplib2 模块:

    import httplib2 
    cert = 'cacerts.txt' 
    http = httplib2.Http(ca_certs=cert) 
    http.request(url)
    

    或者如果您的 httplib2 版本是 0.8,您可以创建一个文件应该命名为 ca_certs_locater.py,并定义一个 get 函数,ca_certs 文件的返回路径.

    Or if your httplib2 version is 0.8, you can create a file that should named ca_certs_locater.py, and define a get function, that return path of ca_certs file.

    def get():
        return 'cacerts.txt'
    

  • 好的,现在对于您的错误,对于 twilio 模块,它使用 httplib2cacert.pem 在:

    Ok, now for your error, and for twilio module, it use httplib2, and cacert.pem of it is in:

    .../twilio/conf/cacert.pem

    因此您需要将这个文件添加到 setup.py 中,如上所述.

    So you need to add this file to setup.py as described above.

    但是 twilio 本身有一个名为 get_cert_fileca_cert 文件传递​​给 httplib2.

    But twilio itself has a function with name get_cert_file that pass ca_cert file to httplib2.

    我认为如果您使用上述的 ca_certs_locater.py,它也适用于此,但如果没有,你还有一个 ugly 选项,所以你可以修改 twilioget_cert_file 函数:

    I think if you using ca_certs_locater.py that described above, it also will works for that, but if not, you have yet an ugly option, so you can monkey patch get_cert_file function of twilio:

    from twilio.rest.resources.base import get_cert_file
    get_cert_file = lambda: 'cacert.pem'
    

    请注意,这可能是 twilio 甚至 py2exePyInstaller 的问题.

    Note that this may an issue for twilio or even for py2exe or PyInstaller .

    这篇关于编译后的 Twilio Python 模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆