File size: 3,885 Bytes
5b9a0e6 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
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.")
```
""")
|