awacke1's picture
Update app.py
14b4ddf verified
raw
history blame contribute delete
No virus
3.89 kB
import streamlit as st
st.markdown("""
# Level 3 Internet Communication Protocols
# πŸ“§ SMTP
- 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.
# πŸ’¬ XMPP
- 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.
# 🌐 HTTP
- **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.
""")
st.markdown("""#Code:
```
import streamlit as st
import smtplib
from email.message import EmailMessage
import http.client
import requests
# SMTP Example (Simplified)
def smtp_client(server, port, from_email, to_email, subject, message):
msg = EmailMessage()
msg.set_content(message)
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
try:
with smtplib.SMTP(server, port) as smtp:
smtp.send_message(msg)
return "Email sent successfully!"
except Exception as e:
return f"Failed to send email: {e}"
# HTTP Example
def http_client(url):
try:
response = requests.get(url)
return response.text[:500] # Return the first 500 characters for brevity
except Exception as e:
return f"Failed to retrieve the URL: {e}"
# Streamlit UI
st.title("Internet Communication Protocols Demo 🌐")
# SMTP Section
st.header("SMTP Demo πŸ“§")
smtp_server = st.text_input("SMTP Server", value="smtp.example.com")
smtp_port = st.text_input("SMTP Port", value="587")
from_email = st.text_input("From Email", value="from@example.com")
to_email = st.text_input("To Email", value="to@example.com")
subject = st.text_input("Subject", value="Test Email")
message = st.text_area("Message", value="This is a test email.")
if st.button("Send Email"):
result = smtp_client(smtp_server, smtp_port, from_email, to_email, subject, message)
st.success(result)
# HTTP Section
st.header("HTTP Demo 🌐")
url = st.text_input("URL to fetch", value="http://example.com")
if st.button("Fetch URL"):
result = http_client(url)
st.text_area("Response", value=result, height=250)
```
```
import streamlit as st
from slixmpp import ClientXMPP
from slixmpp.exceptions import IqError, IqTimeout
class SendMsgBot(ClientXMPP):
def __init__(self, jid, password, recipient, message):
ClientXMPP.__init__(self, jid, password)
self.recipient = recipient
self.msg = message
self.add_event_handler("session_start", self.session_start)
def session_start(self, event):
self.send_presence()
self.get_roster()
try:
self.send_message(mto=self.recipient, mbody=self.msg, mtype='chat')
except IqError as err:
st.error(f"Could not send message: {err.iq['error']['text']}")
self.disconnect()
except IqTimeout:
st.error("Server not responding")
self.disconnect()
else:
st.success("Message sent successfully!")
self.disconnect()
# XMPP Section in Streamlit UI
st.header("XMPP Demo πŸ’¬")
jid = st.text_input("Jabber ID", value="user@server.com")
password = st.text_input("Password", type="password")
recipient = st.text_input("Recipient JID", value="friend@server.com")
message = st.text_area("Message to send")
if st.button("Send XMPP Message"):
if jid and password and recipient and message:
xmpp = SendMsgBot(jid, password, recipient, message)
xmpp.connect()
xmpp.process(forever=False)
else:
st.error("Please fill in all fields.")
```
""")