Hallo,
UnseenInfo = imap.status('INBOX', "(UNSEEN)")
liefert mir folgendes Ergebnis, was ok ist.
('OK', [b'"INBOX" (UNSEEN 0)'])
Jetzt möchte ich aber das, wenn Mails vorhanden sind, die Meldung erscheint: Sie haben Post bzw keine Post.
Da erhalte ich aber immer den Text "Sie haben Post"
nseenInfo = imap.status('INBOX', "(UNSEEN)")
if "0" in nseenInfo:
print("Sie haben keine Post")
else:
print("Sie haben Post")
String durchsuchen
@achim55: UnseenInfo ist ein Tuple aus einem String und einer Liste, wobei die Liste aus einem Element besteht, das einen Bytestring enthält. Dein Test wäre also »b'0' in Unseeninfo[1][0]« wobei dann auch bei 10 Mails für Dich keine Post da wäre.
@achim55: Das liegt daran das Du da keine Zeichenkette hast sondern ein Tupel mit einer Zeichenkette und einer Liste die ein `bytes`-Objekt enthält. Und letzteres willst Du anscheinend durchsuchen. Aber vielleicht nicht nach einem 0-Zeichen, denn wenn man 10 Mails hat, würde Dein Test auf dem richtigen Objekt auch ergeben das keine Mails da sind.
Das löst man entweder mit einem passenden regulären Ausdruck oder mit den Python-Boardmitteln für Strings. Mein Beispiel zeigt letzteres:
Dies trennt den String an den Leerzeichen, nimmt sich anschließend immer das letzte Stück und prüft, ob es mit einer Null beginnt.
Code: Alles auswählen
inbox_info = b'"INBOX" (UNSEEN 0)'
inbox_info.split()[-1].startswith(b'0')
Wenn man schon ein bestimmtes Muster erwartet, sollte man auch explizit darauf prüfen, nicht dass sich das ändert und die 0 plötzlich eine ganz andere Bedeutung bekommt; dann ist wenigstens gegeben, dass eine Meldung erscheint:
Code: Alles auswählen
import re
inbox_info = b'"INBOX" (UNSEEN 0)'
unseen = re.search(rb"\(UNSEEN\s+(\d+)\)", inbox_info, re.I)
if not unseen:
print("Format unbekannt: {}".format(inbox_info))
else:
print("Ungelesene Mails: {}".format(int(unseen.group(1))))
Man könnte auch ein Modul verwenden was das parsen schon erledigt, statt das Rad neu zu erfinden. `imapclient` benutze ich für so etwas immer. Da bekommt man ein Wörterbuch zurück:
Code: Alles auswählen
In [11]: c.folder_status('INBOX')
Out[11]:
{'MESSAGES': 1679,
'RECENT': 0,
'UIDNEXT': 89483,
'UIDVALIDITY': 1283323506,
'UNSEEN': 682}
In [12]: c.folder_status('INBOX', 'UNSEEN')
Out[12]: {'UNSEEN': 682}
Ich bin ein absoluter Neuling und das Anzeigen von neuen Mails ist mein erstes Projekt
Das mit dem imapclient hört sich sinnig an. Leider spukt Goo** nicht viele Beispiele aus. Kennt einer eine gute Seite mit Codes bzw. Erklärungen wie ich imapclient nutzen muss?
Danke
Das mit dem imapclient hört sich sinnig an. Leider spukt Goo** nicht viele Beispiele aus. Kennt einer eine gute Seite mit Codes bzw. Erklärungen wie ich imapclient nutzen muss?
Danke
Benutze folgenden Befehl:achim55 hat geschrieben:Wie installiere ich den IMAPClient ?
Code: Alles auswählen
pip install imapclient
Hi,
ich habe einen Mac!
Leider kommt immer noch ein Fehlermeldung, das er das Modul nicht finden kann.
from __future__ import unicode_literals
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
print('%d messages in INBOX' % select_info['EXISTS'])
Das Ergebnis:
Traceback (most recent call last):
File "/Users/achim/Desktop/Untitled.py", line 4, in <module>
from imapclient import IMAPClient
ImportError: No module named 'imapclient'
Erst einmal habe ich pip installiert : sudo easy_install pip
Dann sudo pip install imapclient
The directory '/Users/achim/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/achim/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting imapclient
Downloading IMAPClient-1.0.1-py2.py3-none-any.whl (68kB)
100% |████████████████████████████████| 69kB 1.3MB/s
Collecting pyopenssl>=0.15.1 (from imapclient)
Downloading pyOpenSSL-0.15.1-py2.py3-none-any.whl (102kB)
100% |████████████████████████████████| 106kB 1.3MB/s
Collecting backports.ssl>=0.0.9 (from imapclient)
Downloading backports.ssl-0.0.9.tar.gz
Requirement already satisfied (use --upgrade to upgrade): six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from imapclient)
Collecting mock==1.3.0 (from imapclient)
Downloading mock-1.3.0-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 57kB 1.5MB/s
Collecting cryptography>=0.7 (from pyopenssl>=0.15.1->imapclient)
Downloading cryptography-1.2.2-cp27-none-macosx_10_10_intel.whl (2.3MB)
100% |████████████████████████████████| 2.3MB 150kB/s
Collecting funcsigs (from mock==1.3.0->imapclient)
Downloading funcsigs-0.4-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock==1.3.0->imapclient)
Downloading pbr-1.8.1-py2.py3-none-any.whl (89kB)
100% |████████████████████████████████| 90kB 1.4MB/s
Collecting enum34 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading enum34-1.1.2.tar.gz (46kB)
100% |████████████████████████████████| 49kB 1.5MB/s
Collecting ipaddress (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading ipaddress-1.0.16-py27-none-any.whl
Collecting pyasn1>=0.1.8 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading pyasn1-0.1.9-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): setuptools>=1.0 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Collecting idna>=2.0 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading idna-2.0-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 1.2MB/s
Collecting cffi>=1.4.1 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading cffi-1.5.2-cp27-cp27m-macosx_10_10_intel.whl (208kB)
100% |████████████████████████████████| 208kB 792kB/s
Collecting pycparser (from cffi>=1.4.1->cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading pycparser-2.14.tar.gz (223kB)
100% |████████████████████████████████| 225kB 700kB/s
Installing collected packages: enum34, ipaddress, pyasn1, idna, pycparser, cffi, cryptography, pyopenssl, backports.ssl, funcsigs, pbr, mock, imapclient
Running setup.py install for enum34 ... done
Running setup.py install for pycparser ... done
Found existing installation: pyOpenSSL 0.13.1
DEPRECATION: Uninstalling a distutils installed project (pyopenssl) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
Uninstalling pyOpenSSL-0.13.1:
Successfully uninstalled pyOpenSSL-0.13.1
Running setup.py install for backports.ssl ... done
Successfully installed backports.ssl-0.0.9 cffi-1.5.2 cryptography-1.2.2 enum34-1.1.2 funcsigs-0.4 idna-2.0 imapclient-1.0.1 ipaddress-1.0.16 mock-1.3.0 pbr-1.8.1 pyasn1-0.1.9 pycparser-2.14 pyopenssl-0.15.1
ich habe einen Mac!
Leider kommt immer noch ein Fehlermeldung, das er das Modul nicht finden kann.
from __future__ import unicode_literals
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
print('%d messages in INBOX' % select_info['EXISTS'])
Das Ergebnis:
Traceback (most recent call last):
File "/Users/achim/Desktop/Untitled.py", line 4, in <module>
from imapclient import IMAPClient
ImportError: No module named 'imapclient'
Erst einmal habe ich pip installiert : sudo easy_install pip
Dann sudo pip install imapclient
The directory '/Users/achim/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/achim/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting imapclient
Downloading IMAPClient-1.0.1-py2.py3-none-any.whl (68kB)
100% |████████████████████████████████| 69kB 1.3MB/s
Collecting pyopenssl>=0.15.1 (from imapclient)
Downloading pyOpenSSL-0.15.1-py2.py3-none-any.whl (102kB)
100% |████████████████████████████████| 106kB 1.3MB/s
Collecting backports.ssl>=0.0.9 (from imapclient)
Downloading backports.ssl-0.0.9.tar.gz
Requirement already satisfied (use --upgrade to upgrade): six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from imapclient)
Collecting mock==1.3.0 (from imapclient)
Downloading mock-1.3.0-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 57kB 1.5MB/s
Collecting cryptography>=0.7 (from pyopenssl>=0.15.1->imapclient)
Downloading cryptography-1.2.2-cp27-none-macosx_10_10_intel.whl (2.3MB)
100% |████████████████████████████████| 2.3MB 150kB/s
Collecting funcsigs (from mock==1.3.0->imapclient)
Downloading funcsigs-0.4-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock==1.3.0->imapclient)
Downloading pbr-1.8.1-py2.py3-none-any.whl (89kB)
100% |████████████████████████████████| 90kB 1.4MB/s
Collecting enum34 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading enum34-1.1.2.tar.gz (46kB)
100% |████████████████████████████████| 49kB 1.5MB/s
Collecting ipaddress (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading ipaddress-1.0.16-py27-none-any.whl
Collecting pyasn1>=0.1.8 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading pyasn1-0.1.9-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): setuptools>=1.0 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Collecting idna>=2.0 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading idna-2.0-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 1.2MB/s
Collecting cffi>=1.4.1 (from cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading cffi-1.5.2-cp27-cp27m-macosx_10_10_intel.whl (208kB)
100% |████████████████████████████████| 208kB 792kB/s
Collecting pycparser (from cffi>=1.4.1->cryptography>=0.7->pyopenssl>=0.15.1->imapclient)
Downloading pycparser-2.14.tar.gz (223kB)
100% |████████████████████████████████| 225kB 700kB/s
Installing collected packages: enum34, ipaddress, pyasn1, idna, pycparser, cffi, cryptography, pyopenssl, backports.ssl, funcsigs, pbr, mock, imapclient
Running setup.py install for enum34 ... done
Running setup.py install for pycparser ... done
Found existing installation: pyOpenSSL 0.13.1
DEPRECATION: Uninstalling a distutils installed project (pyopenssl) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
Uninstalling pyOpenSSL-0.13.1:
Successfully uninstalled pyOpenSSL-0.13.1
Running setup.py install for backports.ssl ... done
Successfully installed backports.ssl-0.0.9 cffi-1.5.2 cryptography-1.2.2 enum34-1.1.2 funcsigs-0.4 idna-2.0 imapclient-1.0.1 ipaddress-1.0.16 mock-1.3.0 pbr-1.8.1 pyasn1-0.1.9 pycparser-2.14 pyopenssl-0.15.1
@achim55: Die Ausgaben sehen aber so aus als wenn die Installation erfolgreich war. :K
so sehe ich das auch
Ich habe gerade mal paramiko installiert, weil ich das für eine SSH Verbindung brauche.
Das gleiche Problem
>>> import paramiko
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import paramiko
ImportError: No module named 'paramiko'
>>>
Paramiko habe ich, zum testen, mal anders installiert.
Entpackt und dann sudo easy_install ./
Lief auch ohne Fehler durch.
Was kann das sein ?
Ich habe gerade mal paramiko installiert, weil ich das für eine SSH Verbindung brauche.
Das gleiche Problem
>>> import paramiko
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import paramiko
ImportError: No module named 'paramiko'
>>>
Paramiko habe ich, zum testen, mal anders installiert.
Entpackt und dann sudo easy_install ./
Lief auch ohne Fehler durch.
Was kann das sein ?
@achim55: Kann es sein das Du mehr als einen Python-Interpreter installiert hast?
das wird es sein!
Der hat mir imapclient nach /Library/Python/2.7 installiert.
Ich habe aber die Version 3.5.1 unter /Library/Frameworks/python.framework/version/3.5
Wie kann ich das den jetzt "geradebiegen" das der imapclient unter 3.5 installiert wird?
Der hat mir imapclient nach /Library/Python/2.7 installiert.
Ich habe aber die Version 3.5.1 unter /Library/Frameworks/python.framework/version/3.5
Wie kann ich das den jetzt "geradebiegen" das der imapclient unter 3.5 installiert wird?
Mehrere Python Versionen nebeneinander sind auch auf dem Mac kein Problem. Du solltest mit Virtuellen Environments arbeiten. Lege Dir eins für Python 3.5 an und installiere von dort aus per pip was Du braucht.achim55 hat geschrieben:Wie kann ich das den jetzt "geradebiegen" das der imapclient unter 3.5 installiert wird?
https://docs.python.org/3/using/scripts ... ght=pyvenv
Oder alternativ:
Wenn das nicht geht:
Code: Alles auswählen
pip3 install imapclient
Code: Alles auswählen
python3 -m pip install imapclient
so langsam kommen wir der Sache näher
Es klappte mit: python3 -m pip install imapclient
Ich bekomme eine Fehlermeldung beim ausführen
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
print('%d messages in INBOX' % select_info['EXISTS'])
KeyError: 'EXISTS'
Es klappte mit: python3 -m pip install imapclient
Ich bekomme eine Fehlermeldung beim ausführen
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
print('%d messages in INBOX' % select_info['EXISTS'])
KeyError: 'EXISTS'
ok, hier der Aufruf.
Code: Alles auswählen
from __future__ import unicode_literals
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
print('%d messages in INBOX' % select_info['EXISTS'])
Zuletzt geändert von Anonymous am Freitag 19. Februar 2016, 19:49, insgesamt 1-mal geändert.
Grund: Quelltext in Code-Tags gesetzt.
Grund: Quelltext in Code-Tags gesetzt.