-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
54 lines (46 loc) · 1.43 KB
/
server.py
File metadata and controls
54 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import email
import imaplib
import smtplib
from email.mime.text import MIMEText
from dotenv import load_dotenv
import os
from datetime import datetime
load_dotenv()
EMAIL = os.getenv("EMAIL")
PASSWORD = os.getenv("PASSWORD")
#imap for receiving email, smtp for sending email
smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp.login(EMAIL, PASSWORD)
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(EMAIL, PASSWORD)
def send_command(command):
msg = MIMEText(command)
msg['Subject'] = 'Command'
msg['From'] = EMAIL
msg['To'] = EMAIL
smtp.sendmail(EMAIL, EMAIL, msg.as_string())
def read_responses():
waiting = True
while waiting:
mail.select('inbox')
_, messages = mail.search(None, '(UNSEEN SUBJECT "Response")')
if messages is not None:
for num in messages[0].split():
_, data = mail.fetch(num, '(RFC822)')
raw_email = data[0][1]
msg = email.message_from_bytes(raw_email)
print(msg.get_payload(decode=True).decode())
mail.store(num, '+FLAGS', '\\Seen')
mail.expunge()
waiting = False
def is_blank (string):
return not (string and string.strip())
def handle_input():
cmd = input('> ')
while cmd != 'quit':
if not is_blank(cmd):
send_command(cmd)
read_responses()
cmd = input('> ')
if '__main__' == __name__:
handle_input()