#!/usr/bin/python import string words=string.split(’hello world again’) print words
#!/usr/bin/python file = open(’newfile.txt’, ’w’) file.write(’hello world in the new file\n’) file.write(’and another line\n’) file.close()
#!/usr/bin/python
file = open(’newfile.txt’, ’w’)
file.writelines([’hello world in the new file\n’,
’and another line\n’])
file.close()
#!/usr/bin/python
file = open(’newfile.txt’, ’r’)
for line in file.readlines():
print line
#!/usr/bin/python import httplib h = httplib.HTTP(’floppsie.comp.glam.ac.uk’) h.putrequest(’GET’, ’/index.html’) h.putheader(’Accept’, ’text/html’) h.putheader(’Accept’, ’text/plain’) h.endheaders() errcode, errmsg, headers = h.getreply() print "return value is", errcode # Should be 200 f = h.getfile() data = f.read() f.close() print data
#!/usr/bin/python
import httplib, urllib
params = urllib.urlencode({’spam’: 1, ’eggs’: 2,
’bacon’: 0})
h = httplib.HTTP("www.musi-cal.com:80")
h.putrequest("POST", "/cgi-bin/query")
h.putheader("Content-length", "%d" % len(params))
h.putheader(’Accept’, ’text/plain’)
h.putheader(’Host’, ’www.musi-cal.com’)
h.endheaders()
h.send(paramstring)
reply, msg, hdrs = h.getreply()
print errcode # should be 200
data = h.getfile().read() # get the raw HTML
#!/usr/bin/python import sys from socket import * 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
#!/usr/bin/python
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 1:
# wait for next client to connect
connection, address = s.accept()
while 1:
data = connection.recv(1024)
if data:
connection.send(’echo -> ’ + data)
else:
break
connection.close()
#!/usr/bin/python
import getpass, imaplib, string
m = imaplib.IMAP4(’moppsy.comp.glam.ac.uk’)
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/python
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/python
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]
if __name__ == "__main__":
main()
#!/usr/bin/python
import sys
def multiplyby10(value):
return value+’0’
if __name__ == "__main__":
if len(sys.argv) == 2:
print ’testing the times module’
print multiplyby10(sys.argv[1])
#!/usr/bin/python
import times, sys
if len(sys.argv) == 2:
print ’importing the times module’
print times.multiplyby10(sys.argv[1])
./prog.py 12 importing the times module 120
./times.py 12 testing the times module 120
This document was produced using groff-1.19.