Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b83ec8f

Browse files
committed
Initial revision
1 parent c99a4f9 commit b83ec8f

5 files changed

Lines changed: 243 additions & 0 deletions

File tree

Demo/scripts/makedir.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#! /usr/local/python
2+
3+
# Like mkdir, but also make intermediate directories if necessary.
4+
# It is not an error if the given directory already exists (as long
5+
# as it is a directory).
6+
# Errors are not treated specially -- you just get a Python exception.
7+
8+
import sys, os
9+
10+
def main():
11+
for p in sys.argv[1:]:
12+
makedirs(p)
13+
14+
def makedirs(p):
15+
if not os.path.isdir(p):
16+
head, tail = os.path.split(p)
17+
makedirs(head)
18+
os.mkdir(p, 0777)
19+
20+
main()

Demo/scripts/mkrcs.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#! /ufs/guido/bin/sgi/python
2+
#! /usr/local/python
3+
4+
# A rather specialized script to make sure that a symbolic link named
5+
# RCS exists pointing to a real RCS directory in a parallel tree
6+
# referenced as RCStree in an ancestor directory.
7+
# (I use this because I like my RCS files to reside on a physically
8+
# different machine).
9+
10+
import os
11+
12+
def main():
13+
rcstree = 'RCStree'
14+
rcs = 'RCS'
15+
if os.path.islink(rcs):
16+
print `rcs`, 'is a symlink to', `os.readlink(rcs)`
17+
return
18+
if os.path.isdir(rcs):
19+
print `rcs`, 'is an ordinary directory'
20+
return
21+
if os.path.exists(rcs):
22+
print `rcs`, 'is a file?!?!'
23+
return
24+
#
25+
p = os.getcwd()
26+
up = ''
27+
down = ''
28+
# Invariants:
29+
# (1) join(p, down) is the current directory
30+
# (2) up is the same directory as p
31+
# Ergo:
32+
# (3) join(up, down) is the current directory
33+
#print 'p =', `p`
34+
while not os.path.isdir(os.path.join(p, rcstree)):
35+
head, tail = os.path.split(p)
36+
#print 'head =', `head`, '; tail =', `tail`
37+
if not tail:
38+
print 'Sorry, no ancestor dir contains', `rcstree`
39+
return
40+
p = head
41+
up = os.path.join(os.pardir, up)
42+
down = os.path.join(tail, down)
43+
#print 'p =', `p`, '; up =', `up`, '; down =', `down`
44+
there = os.path.join(up, rcstree)
45+
there = os.path.join(there, down)
46+
there = os.path.join(there, rcs)
47+
if os.path.isdir(there):
48+
print `there`, 'already exists'
49+
else:
50+
print 'making', `there`
51+
makedirs(there)
52+
print 'making symlink', `rcs`, '->', `there`
53+
os.symlink(there, rcs)
54+
55+
def makedirs(p):
56+
if not os.path.isdir(p):
57+
head, tail = os.path.split(p)
58+
makedirs(head)
59+
os.mkdir(p, 0777)
60+
61+
main()

Demo/sockets/broadcast.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Send UDP broadcast packets
2+
3+
MYPORT = 50000
4+
5+
import sys, time
6+
from socket import *
7+
8+
s = socket(AF_INET, SOCK_DGRAM)
9+
s.bind('', 0)
10+
s.allowbroadcast(1)
11+
12+
while 1:
13+
data = `time.time()` + '\n'
14+
s.sendto(data, ('<broadcast>', MYPORT))
15+
time.sleep(2)
16+
17+

Demo/sockets/ftp.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# A simple FTP client.
2+
#
3+
# The information to write this program was gathered from RFC 959,
4+
# but this is not a complete implementation! Yet it shows how a simple
5+
# FTP client can be built, and you are welcome to extend it to suit
6+
# it to your needs...
7+
8+
9+
import sys, posix, string
10+
from socket import *
11+
12+
13+
BUFSIZE = 1024
14+
15+
# Default port numbers used by the FTP protocol.
16+
#
17+
FTP_PORT = 21
18+
FTP_DATA_PORT = FTP_PORT - 1
19+
20+
# Change the data port to something not needing root permissions.
21+
#
22+
FTP_DATA_PORT = FTP_DATA_PORT + 50000
23+
24+
25+
# Main program (called at the end of this file).
26+
#
27+
def main():
28+
hostname = sys.argv[1]
29+
control(hostname)
30+
31+
32+
# Control process (user interface and user protocol interpreter).
33+
#
34+
def control(hostname):
35+
#
36+
# Create control connection
37+
#
38+
s = socket(AF_INET, SOCK_STREAM)
39+
s.connect(hostname, FTP_PORT)
40+
f = s.makefile('r') # Reading the replies is easier from a file...
41+
#
42+
# Control loop
43+
#
44+
r = None
45+
while 1:
46+
code = getreply(f)
47+
if code in ('221', 'EOF'): break
48+
if code == '150':
49+
getdata(r)
50+
code = getreply(f)
51+
r = None
52+
if not r:
53+
r = newdataport(s, f)
54+
cmd = getcommand()
55+
if not cmd: break
56+
s.send(cmd + '\r\n')
57+
58+
59+
# Create a new data port and send a PORT command to the server for it.
60+
# (Cycle through a number of ports to avoid problems with reusing
61+
# a port within a short time.)
62+
#
63+
cycle = [0]
64+
#
65+
def newdataport(s, f):
66+
port = cycle[0]
67+
cycle[0] = (port+1) % 16
68+
port = port + FTP_DATA_PORT
69+
r = socket(AF_INET, SOCK_STREAM)
70+
r.bind(gethostbyname(gethostname()), port)
71+
r.listen(0)
72+
sendportcmd(s, f, port)
73+
return r
74+
75+
76+
# Send an appropriate port command.
77+
#
78+
def sendportcmd(s, f, port):
79+
hostname = gethostname()
80+
hostaddr = gethostbyname(hostname)
81+
hbytes = string.splitfields(hostaddr, '.')
82+
pbytes = [`port/256`, `port%256`]
83+
bytes = hbytes + pbytes
84+
cmd = 'PORT ' + string.joinfields(bytes, ',')
85+
s.send(cmd + '\r\n')
86+
code = getreply(f)
87+
88+
89+
# Process an ftp reply and return the 3-digit reply code (as a string).
90+
# The reply should be a line of text starting with a 3-digit number.
91+
# If the 4th char is '-', it is a multi-line reply and is
92+
# terminate by a line starting with the same 3-digit number.
93+
# Any text while receiving the reply is echoed to the file.
94+
#
95+
def getreply(f):
96+
line = f.readline()
97+
if not line: return 'EOF'
98+
print line,
99+
code = line[:3]
100+
if line[3:4] == '-':
101+
while 1:
102+
line = f.readline()
103+
if not line: break # Really an error
104+
print line,
105+
if line[:3] == code: break
106+
return code
107+
108+
109+
# Get the data from the data connection.
110+
#
111+
def getdata(r):
112+
print '(accepting data connection)'
113+
conn, host = r.accept()
114+
print '(data connection accepted)'
115+
while 1:
116+
data = conn.recv(BUFSIZE)
117+
if not data: break
118+
sys.stdout.write(data)
119+
print '(end of data connection)'
120+
121+
# Get a command from the user.
122+
#
123+
def getcommand():
124+
try:
125+
return raw_input('ftp> ')
126+
except EOFError:
127+
return ''
128+
129+
130+
# Call the main program.
131+
#
132+
main()

Demo/sockets/radio.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Receive UDP packets transmitted by a broadcasting service
2+
3+
MYPORT = 50000
4+
5+
import sys
6+
from socket import *
7+
8+
s = socket(AF_INET, SOCK_DGRAM)
9+
s.bind('', MYPORT)
10+
11+
while 1:
12+
data = s.recv(1500)
13+
sys.stdout.write(data)

0 commit comments

Comments
 (0)