Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
st.markdown("""
|
4 |
+
# Level 3 Internet Communication Protocols
|
5 |
+
|
6 |
+
# π§ SMTP
|
7 |
+
- Simple Mail Transfer Protocol: The standard protocol used for sending email messages over the internet. It defines how email clients and servers communicate and deliver messages.
|
8 |
+
|
9 |
+

|
10 |
+
|
11 |
+
# π¬ XMPP
|
12 |
+
- Extensible Messaging and Presence Protocol: A communication protocol used for instant messaging, presence information, and contact list management. It allows for interoperability between different messaging platforms.
|
13 |
+
|
14 |
+

|
15 |
+
|
16 |
+
# π HTTP
|
17 |
+
- **Hypertext Transfer Protocol**: The foundation of data communication on the World Wide Web. It defines how web browsers and servers exchange information, enabling the retrieval and display of web pages.
|
18 |
+
|
19 |
+

|
20 |
+
|
21 |
+
""")
|
22 |
+
|
23 |
+
|
24 |
+
st.markdown("""#Code:
|
25 |
+
|
26 |
+
```
|
27 |
+
import streamlit as st
|
28 |
+
import smtplib
|
29 |
+
from email.message import EmailMessage
|
30 |
+
import http.client
|
31 |
+
import requests
|
32 |
+
|
33 |
+
# SMTP Example (Simplified)
|
34 |
+
def smtp_client(server, port, from_email, to_email, subject, message):
|
35 |
+
msg = EmailMessage()
|
36 |
+
msg.set_content(message)
|
37 |
+
msg['Subject'] = subject
|
38 |
+
msg['From'] = from_email
|
39 |
+
msg['To'] = to_email
|
40 |
+
|
41 |
+
try:
|
42 |
+
with smtplib.SMTP(server, port) as smtp:
|
43 |
+
smtp.send_message(msg)
|
44 |
+
return "Email sent successfully!"
|
45 |
+
except Exception as e:
|
46 |
+
return f"Failed to send email: {e}"
|
47 |
+
|
48 |
+
# HTTP Example
|
49 |
+
def http_client(url):
|
50 |
+
try:
|
51 |
+
response = requests.get(url)
|
52 |
+
return response.text[:500] # Return the first 500 characters for brevity
|
53 |
+
except Exception as e:
|
54 |
+
return f"Failed to retrieve the URL: {e}"
|
55 |
+
|
56 |
+
# Streamlit UI
|
57 |
+
st.title("Internet Communication Protocols Demo π")
|
58 |
+
|
59 |
+
# SMTP Section
|
60 |
+
st.header("SMTP Demo π§")
|
61 |
+
smtp_server = st.text_input("SMTP Server", value="smtp.example.com")
|
62 |
+
smtp_port = st.text_input("SMTP Port", value="587")
|
63 |
+
from_email = st.text_input("From Email", value="[email protected]")
|
64 |
+
to_email = st.text_input("To Email", value="[email protected]")
|
65 |
+
subject = st.text_input("Subject", value="Test Email")
|
66 |
+
message = st.text_area("Message", value="This is a test email.")
|
67 |
+
if st.button("Send Email"):
|
68 |
+
result = smtp_client(smtp_server, smtp_port, from_email, to_email, subject, message)
|
69 |
+
st.success(result)
|
70 |
+
|
71 |
+
# HTTP Section
|
72 |
+
st.header("HTTP Demo π")
|
73 |
+
url = st.text_input("URL to fetch", value="http://example.com")
|
74 |
+
if st.button("Fetch URL"):
|
75 |
+
result = http_client(url)
|
76 |
+
st.text_area("Response", value=result, height=250)
|
77 |
+
```
|
78 |
+
|
79 |
+
```
|
80 |
+
import streamlit as st
|
81 |
+
from slixmpp import ClientXMPP
|
82 |
+
from slixmpp.exceptions import IqError, IqTimeout
|
83 |
+
|
84 |
+
class SendMsgBot(ClientXMPP):
|
85 |
+
def __init__(self, jid, password, recipient, message):
|
86 |
+
ClientXMPP.__init__(self, jid, password)
|
87 |
+
self.recipient = recipient
|
88 |
+
self.msg = message
|
89 |
+
self.add_event_handler("session_start", self.session_start)
|
90 |
+
|
91 |
+
def session_start(self, event):
|
92 |
+
self.send_presence()
|
93 |
+
self.get_roster()
|
94 |
+
try:
|
95 |
+
self.send_message(mto=self.recipient, mbody=self.msg, mtype='chat')
|
96 |
+
except IqError as err:
|
97 |
+
st.error(f"Could not send message: {err.iq['error']['text']}")
|
98 |
+
self.disconnect()
|
99 |
+
except IqTimeout:
|
100 |
+
st.error("Server not responding")
|
101 |
+
self.disconnect()
|
102 |
+
else:
|
103 |
+
st.success("Message sent successfully!")
|
104 |
+
self.disconnect()
|
105 |
+
|
106 |
+
# XMPP Section in Streamlit UI
|
107 |
+
st.header("XMPP Demo π¬")
|
108 |
+
jid = st.text_input("Jabber ID", value="[email protected]")
|
109 |
+
password = st.text_input("Password", type="password")
|
110 |
+
recipient = st.text_input("Recipient JID", value="[email protected]")
|
111 |
+
message = st.text_area("Message to send")
|
112 |
+
|
113 |
+
if st.button("Send XMPP Message"):
|
114 |
+
if jid and password and recipient and message:
|
115 |
+
xmpp = SendMsgBot(jid, password, recipient, message)
|
116 |
+
xmpp.connect()
|
117 |
+
xmpp.process(forever=False)
|
118 |
+
else:
|
119 |
+
st.error("Please fill in all fields.")
|
120 |
+
|
121 |
+
```
|
122 |
+
|
123 |
+
""")
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
|