Skip to content

Instantly share code, notes, and snippets.

@Kenan7
Last active June 24, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kenan7/794dcf69943d79d88a4cb75eff267d51 to your computer and use it in GitHub Desktop.
Save Kenan7/794dcf69943d79d88a4cb75eff267d51 to your computer and use it in GitHub Desktop.

Network Programming

Mirkenan Kazımzade - 17155960


Homework 9

Question 9-1

Sending email with zip attachment using smtplib

#Imports here

server = 'smtp.gmail.com'
port = 587
username = 'fake@gmail.com'
password = 'fake'
sender = username
isGMAIL = True

def sendMsg():
    msg = MIMEMultipart()
    msg['Subject'] = SUBJECT
    msg['From'] = sender
    msg['To'] = RECIPIENTS

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(open_zip_file_to_read)
    Encode(part)
    part.add_header(headers)
    msg.attach(part)

    smtp = smtplib.SMTP(server, port)
    if isGMAIL:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
    smtp.login(username, password)
    smtp.sendmail(sender, RECIPIENTS, msg.as_string())
    smtp.close()


zip(DIRECTORY_OF_IMAGES, NAME_OF_DESTINATION_ARCHIVE) # defined function

try:
    sendMsg()
except Exception:
    sys.exit('mail failed; %s' % str(exc))

Retrieve email using poplib

#imports here

mServer = poplib.POP3('mail.sfcn.org')

#Login to mail server
mServer.user(getpass.getuser())
mServer.pass_(getpass.getpass())

#Get the number of mail messages
numMessages = len(mServer.list()[1])

print("You have %d messages." % (numMessages))
print("Message List:")

#List the subject line of each message
for message_list in range(numMessages) :
    for msg in mServer.retr(message_list + 1)[1]:
        if msg.startswith('Subject'):
            print('\t' + msg)
            break

mServer.quit()

Question 9-2

Send image file using ftplib after login

from ftplib import FTP
from pathlib import Path

file_path = Path('schema.jpg')

with FTP('ftp.server.com', 'USER', 'PWD') as ftp, open(file_path, 'rb') as file:
        ftp.storbinary(f'STOR {file_path.name}', file)

Retrieve file from FTP server

import urllib 

urllib.urlretrieve('ftp://server/path/to/file', 'file')

Homework 10

Question 10-1

What is XML-RPC?

The XML-RPC is a XML based protocol. It is a simple protocol used to exchange information between computer systems over a network. It is a remote procedure call and it uses XML to encode the calls.

screenshot of server-client usage in terminal

XML-RPC server

from xmlrpc.server import SimpleXMLRPCServer

def add(x, y):
    return x + y

def sub(x, y):
    return x - y

srv = SimpleXMLRPCServer(('localhost', 5080))
srv.register_function(add, 'add')
srv.register_function(sub, 'sub')
srv.serve_forever()

XML-RPC client

import xmlrpc.client

with xmlrpc.client.ServerProxy('http://localhost:5080/') as proxy:
    print("sum of 1 and 1 is: %s"  %  str(proxy.add(1, 1)))
    print("subtraction of 2 from 5 is: %s"  %  str(proxy.sub(5, 2)))

Also link to the code: Gist GitHub


Question 10-2

What is SOAP?

SOAP is an acronym for Simple Object Access Protocol. It is an XML-based messaging protocol for exchanging information among computers. SOAP is an application of the XML specification.

Difference between SOAP and XML-RCP

  • SOAP is more verbose, but more capable.
  • RPC, on the other hand, has more python support than that of SOAP.
  • SOAP supports document-level transfer, whereas xml-rpc is more about values transfer, although it can transfer structures such as structs, lists, etc.
  • xml-rpc is really about program to program language agnostic transfer. It primarily goes over http/https. SOAP messages can go over email as well.
  • xml-rpc is more unixy. It lets you do things simply, and when you know what you're doing, it's very fast to deploy quality web services, even when using terminal text editors. Doing SOAP that way is a zoo; you really need a good IDE to make it feasible.
  • Knowing SOAP, though, will look much better on your resume/CV if you're vying for a Fortune 500 IT job.
  • xml-rpc has some issues with non-ascii character sets.
  • XML-RPC does not support named parameters. They must be in correct order. Not sure about SOAP, but think so.

Who's using SOAP?

Google seams to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprise software as well.

ALTERNATIVES

REST

It's not actually messaging protocol but rather architectural approach

ADVANTAGES of REST

  • No expensive tools require to interact with the web service
  • Smaller learning curve
  • Efficient (SOAP uses XML for all messages, REST can use smaller message formats)
  • Fast (no extensive processing required)
  • Closer to other web technologies in design philosophy

ADVANTAGES of SOAP

  • Language, platform, and transport independent (REST requires use of HTTP)
  • Works well in distributed enterprise environments (REST assumes direct point-to-point communication)
  • Standardized
  • Provides significant pre-build extensibility in the form of the WS* standards
  • Built-in error handling
  • Automation when used with certain language products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment