douna3367 2019-05-24 00:40
浏览 161

使用imaplib时Bash vs CMD

I am trying to run a php script that will call a python script that calls imaplib to pull some files from my email. When I run the python script standalone in vscode (bash terminal), it works as designed. When I run the PHP script that calls the python script, the following error is produced.

Traceback (most recent call last):
  File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 43, in <module>
    mail = imaplib.IMAP4_SSL('imap.googlemail.com')
AttributeError: module 'imaplib' has no attribute 'IMAP4_SSL'

As a test, I echoed out the command and pasted it into Git Bash. The files were pulled and added to the folder specified. When I pasted the command into CMD, the error above was produced. What settings am I missing?

I am using XAMPP, VSCode, PHP 7.3.4, Python 3.7.1

From recommendation below I added "import ssl" to my python script, I received the following error:

"Traceback (most recent call last):"
"  File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 2, in <module>"
"    import ssl"
"  File "C:\Users\Garrett\Anaconda3\envs\snowflakes\lib\ssl.py", line 98, in <module>"
"    import _ssl             # if we can't import it, let the error propagate"
"ImportError: DLL load failed: The specified module could not be found."

so then I went to my python environment, and tried to "pip install ssl" and I got the following error.

(snowflakes) C:\Users\Garrett>pip install ssl
Collecting ssl
  Using cached https://files.pythonhosted.org/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\setup.py", line 33
        print 'looking for', f
                          ^
    SyntaxError: Missing parentheses in call to 'print'. Did you mean print('looking for', f)?
    ----------------------------------------
ERROR: Command "python setup.py egg_info" failed with error code 1 in C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\

(snowflakes) C:\Users\Garrett>

Another Update

The following command is what is executed in PHP (exec()). The input folder will contain files pulled from email. The output folder will contain a singular file created from the files pulled from email. If I paste this command into GitBash, everything works as planned. If I paste the following command into CMD, I get the error shown above.

C:/Users/Garrett/Anaconda3/python.exe C:/xampp/htdocs/report/pythonfile/parse_script_og.py C:/xampp/htdocs/report/input/ C:/xampp/htdocs/report/output/out.csv John Doe 06-Jan-2018 16-Jan-2019
  • 写回答

1条回答 默认 最新

  • doujia6503 2019-05-24 00:46
    关注

    IMAP4_SSL is only present if Python's ssl module is available (you can test with import ssl). Try upgrading your server's python version, and/or installing the ssl module.

    https://docs.python.org/3/library/ssl.html
    

    EDIT 1:

    There’s also a subclass for secure connections:

    class imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None)

    This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is a ssl.SSLContext object which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read Security considerations for best practices.

    keyfile and certfile are a legacy alternative to ssl_context - they can point to PEM-formatted private key and certificate chain files for the SSL connection. Note that the keyfile/certfile parameters are mutually exclusive with ssl_context, a ValueError is raised if keyfile/certfile is provided along with ssl_context.

    Changed in version 3.3: ssl_context parameter added.

    Changed in version 3.4: The class now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI).

    Deprecated since version 3.6: keyfile and certfile are deprecated in favor of ssl_context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.

    https://docs.python.org/3/library/imaplib.html

    EDIT 2:

    you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:

    $mystring = exec('/usr/bin/python /home/user/testing.py');
    

    and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.

    Python Script Failing to Execute from PHP exec()

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名