#!/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_SSL(’moppsy.comp.glam.ac.uk’)
m.login(getpass.getuser(), getpass.getpass())
m.select()
typ, data = m.search(None, ’ALL’)
print "typ = ", typ
print "data = ", data
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()
ssh moppsy.comp.glam.ac.uk Login: u012345678 Password: $ mail -s "this is a test" 012345678@localhost this is the body of the message ^D $ exit
#!/usr/bin/python
import sys, getopt
def Usage ():
print "arguments.py [-v][-p][-h]"
try:
optlist, list = getopt.getopt(sys.argv[1:],
’:vph’)
except getopt.GetoptError:
Usage()
print "incorrect argument given"
sys.exit(1)
for opt in optlist:
if opt[0] == ’-h’:
Usage()
if opt[0] == ’-v’:
print "verbose found"
if opt[0] == ’-p’:
print "probeonly found"
python arguments.py -v python arguments.py -h python arguments.py -p python arguments.py -p -v python arguments.py -pv python arguments.py -pvh python arguments.py -pv -h python arguments.py -vp -h python arguments.py -v -p -h python arguments.py -h -phv
#!/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
now copy this program and call it server.py
#!/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()
python server.py
python client.py
hostname
This document was produced using groff-1.19.