#!/usr/bin/python3 from urllib.request import urlretrieve urlretrieve("http://floppsie.comp.glam.ac.uk/index.html", "temp.html")
#!/usr/bin/python3 import os import urllib.request, urllib.parse, urllib.error Version = "3.7.4" filename = "Python-%s.tgz" % Version remoteaddr = "https://www.python.org/ftp/python/%s/" % Version urllib.request.urlretrieve(remoteaddr + filename, filename)
#!/usr/bin/python3 import smtplib, string, sys, time mailserver = "localhost" From = input("From: ").strip () To = input("To: ").strip () Subject = input("Subject: "). strip () Date = time.ctime(time.time()) Header = ("From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n" % (From, To, Date, Subject)) Text = "my message" server = smtplib.SMTP(mailserver) failed = server.sendmail(From, To, Header + Text) server.quit() if failed: print("failed to send mail") else: print("all done..")
#!/usr/bin/python3 import string words=string.split("hello world again") print words
#!/usr/bin/python3 file = open("newfile.txt", "w") file.write("hello world in the new file\n") file.write("and another line\n") file.close()
#!/usr/bin/python3 file = open("newfile.txt", "w") file.writelines(["hello world in the new file\n", "and another line\n"]) file.close()
#!/usr/bin/python3 file = open("newfile.txt", "r") for line in file.readlines(): print(line)
#!/usr/bin/python3 from socket import * myHost = "" myPort = 2000 # create a TCP socket s = socket(AF_INET, SOCK_STREAM) # bind it to the server port number s.bind((myHost, myPort)) # allow 5 pending connections s.listen(5) while True: # wait for next client to connect connection, address = s.accept() while True: data = connection.recv(1024) if data: connection.send("echo -> " + data) else: break connection.close()
#!/usr/bin/python3 import sys from socket import * # serverHost = "localhost" serverHost = "localhost" serverPort = 2000 # create a TCP socket s = socket(AF_INET, SOCK_STREAM) s.connect((serverHost, serverPort)) s.send("Hello world") data = s.recv(1024) print(data)
$ python3 server.py
$ python3 client.py
#!/usr/bin/python3 import getpass, imaplib, string # m = imaplib.IMAP4_SSL("unimail.isd.glam.ac.uk") m = imaplib.IMAP4_SSL("outlook.office365.com") m.login(getpass.getuser(), getpass.getpass()) m.select () typ, data = m.search (None, "ALL") for num in string.split (data[0]): typ, data = m.fetch (num, "(RFC822)") print("Message %s\n%s\n" % (num, data[0][1])) m.logout()
#!/usr/bin/python3 import sys, getopt def Usage (): print("autoftp [-v][-p][-h]") sys.exit(0) optlist, list = getopt.getopt(sys.argv[1:], ":vphf:") print("optlist =", optlist) print("list =", list) for opt in optlist: print(opt) if opt[0] == "-h": Usage() if opt[0] == "-f": print("file found") if opt[0] == "-v": print("verbose found") if opt[0] == "-p": print("probeonly found")
./autoftp2.py -x ... getopt.GetoptError: option -x not recognised
#!/usr/bin/python3 import sys, getopt def Usage (): print("autoftp [-v][-p][-h]") sys.exit(0) try: optlist, list = getopt.getopt(sys.argv[1:], ":vphf:") except getopt.GetoptError: Usage() print("called exception") sys.exit(1) for opt in optlist: print(opt) if opt[0] == "-h": Usage() if opt[0] == "-v": print("verbose found") if opt[0] == "-p": print("probeonly found") if opt[0] == "-f": print("file option found")
./autoftp3.py -x autoftp [-v][-p][-h]
This document was produced using groff-1.22.