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

Skip to content

Commit 22825e8

Browse files
committed
Initial revision
1 parent 2fa5a7f commit 22825e8

6 files changed

Lines changed: 361 additions & 0 deletions

File tree

Demo/sockets/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Contents of this directory:
2+
3+
echosvr.py About the simplest TCP server possible.
4+
finger.py Client for the 'finger' protocol.
5+
telnet.py Client for the 'telnet' protocol.
6+
throughput.py Client and server to measure TCP throughput.
7+
udpecho.py Client and server for the UDP echo protocol.

Demo/sockets/echosvr.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! /usr/local/python
2+
3+
# Python implementation of an 'echo' tcp server: echo all data it receives.
4+
#
5+
# This is the simplest possible server, sevicing a single request only.
6+
7+
import sys
8+
from socket import *
9+
10+
# The standard echo port isn't very useful, it requires root permissions!
11+
# ECHO_PORT = 7
12+
ECHO_PORT = 50000 + 7
13+
BUFSIZE = 1024
14+
15+
def main():
16+
if len(sys.argv) > 1:
17+
port = int(eval(sys.argv[1]))
18+
else:
19+
port = ECHO_PORT
20+
s = socket(AF_INET, SOCK_STREAM)
21+
s.bind('', port)
22+
s.listen(0)
23+
conn, (host, remoteport) = s.accept()
24+
print 'connected by', host, remoteport
25+
while 1:
26+
data = conn.recv(BUFSIZE)
27+
if not data:
28+
break
29+
conn.send(data)
30+
31+
main()

Demo/sockets/finger.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#! /usr/local/python
2+
3+
# Python interface to the Internet finger daemon.
4+
#
5+
# Usage: finger [options] [user][@host] ...
6+
#
7+
# If no host is given, the finger daemon on the local host is contacted.
8+
# Options are passed uninterpreted to the finger daemon!
9+
10+
11+
import sys, string
12+
from socket import *
13+
14+
15+
# Hardcode the number of the finger port here.
16+
# It's not likely to change soon...
17+
#
18+
FINGER_PORT = 79
19+
20+
21+
# Function to do one remote finger invocation.
22+
# Output goes directly to stdout (although this can be changed).
23+
#
24+
def finger(host, args):
25+
s = socket(AF_INET, SOCK_STREAM)
26+
s.connect(host, FINGER_PORT)
27+
s.send(args + '\n')
28+
while 1:
29+
buf = s.recv(1024)
30+
if not buf: break
31+
sys.stdout.write(buf)
32+
sys.stdout.flush()
33+
34+
35+
# Main function: argument parsing.
36+
#
37+
def main():
38+
options = ''
39+
i = 1
40+
while i < len(sys.argv) and sys.argv[i][:1] = '-':
41+
options = options + sys.argv[i] + ' '
42+
i = i+1
43+
args = sys.argv[i:]
44+
if not args:
45+
args = ['']
46+
for arg in args:
47+
if '@' in arg:
48+
at = string.index(arg, '@')
49+
host = arg[at+1:]
50+
arg = arg[:at]
51+
else:
52+
host = ''
53+
finger(host, options + arg)
54+
55+
56+
# Call the main function.
57+
#
58+
main()

Demo/sockets/telnet.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#! /usr/local/python
2+
3+
# Minimal interface to the Internet telnet protocol.
4+
#
5+
# It refuses all telnet options and does not recognize any of the other
6+
# telnet commands, but can still be used to connect in line-by-line mode.
7+
# It's also useful to play with a number of other services,
8+
# like time, finger, smtp and even ftp.
9+
#
10+
# Usage: telnet host [port]
11+
#
12+
# The port may be a service name or a decimal port number;
13+
# it defaults to 'telnet'.
14+
15+
16+
import sys, posix, time
17+
from socket import *
18+
19+
BUFSIZE = 1024
20+
21+
# Telnet protocol characters
22+
23+
IAC = chr(255) # Interpret as command
24+
DONT = chr(254)
25+
DO = chr(253)
26+
WONT = chr(252)
27+
WILL = chr(251)
28+
29+
def main():
30+
host = sys.argv[1]
31+
try:
32+
hostaddr = gethostbyname(host)
33+
except error:
34+
sys.stderr.write(sys.argv[1] + ': bad host name\n')
35+
sys.exit(2)
36+
#
37+
if len(sys.argv) > 2:
38+
servname = sys.argv[2]
39+
else:
40+
servname = 'telnet'
41+
#
42+
if '0' <= servname[:1] <= '9':
43+
port = eval(servname)
44+
else:
45+
try:
46+
port = getservbyname(servname, 'tcp')
47+
except error:
48+
sys.stderr.write(servname + ': bad tcp service name\n')
49+
sys.exit(2)
50+
#
51+
s = socket(AF_INET, SOCK_STREAM)
52+
#
53+
try:
54+
s.connect(host, port)
55+
except error, msg:
56+
sys.stderr.write('connect failed: ' + `msg` + '\n')
57+
sys.exit(1)
58+
#
59+
pid = posix.fork()
60+
#
61+
if pid = 0:
62+
# child -- read stdin, write socket
63+
while 1:
64+
line = sys.stdin.readline()
65+
s.send(line)
66+
else:
67+
# parent -- read socket, write stdout
68+
iac = 0 # Interpret next char as command
69+
opt = '' # Interpret next char as option
70+
while 1:
71+
data = s.recv(BUFSIZE)
72+
if not data:
73+
# EOF; kill child and exit
74+
sys.stderr.write( '(Closed by remote host)\n')
75+
posix.kill(pid, 9)
76+
sys.exit(1)
77+
cleandata = ''
78+
for c in data:
79+
if opt:
80+
print ord(c)
81+
s.send(opt + c)
82+
opt = ''
83+
elif iac:
84+
iac = 0
85+
if c = IAC:
86+
cleandata = cleandata + c
87+
elif c in (DO, DONT):
88+
if c = DO: print '(DO)',
89+
else: print '(DONT)',
90+
opt = IAC + WONT
91+
elif c in (WILL, WONT):
92+
if c = WILL: print '(WILL)',
93+
else: print '(WONT)',
94+
opt = IAC + DONT
95+
else:
96+
print '(command)', ord(c)
97+
elif c = IAC:
98+
iac = 1
99+
print '(IAC)',
100+
else:
101+
cleandata = cleandata + c
102+
sys.stdout.write(cleandata)
103+
sys.stdout.flush()
104+
105+
106+
try:
107+
main()
108+
except KeyboardInterrupt:
109+
pass

Demo/sockets/throughput.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#! /usr/local/python
2+
3+
# Test network throughput.
4+
#
5+
# Usage:
6+
# 1) on host_A: throughput -s [port] # start a server
7+
# 2) on host_B: throughput -c count host_A [port] # start a client
8+
#
9+
# The server will service multiple clients until it is killed.
10+
#
11+
# The client performs one transfer of count*BUFSIZE bytes and
12+
# measures the time it takes (roundtrip!).
13+
14+
15+
import sys, time
16+
from socket import *
17+
18+
MY_PORT = 50000 + 42
19+
20+
BUFSIZE = 1024
21+
22+
23+
def main():
24+
if len(sys.argv) < 2:
25+
usage()
26+
if sys.argv[1] = '-s':
27+
server()
28+
elif sys.argv[1] = '-c':
29+
client()
30+
else:
31+
usage()
32+
33+
34+
def usage():
35+
sys.stdout = sys.stderr
36+
print 'Usage: (on host_A) throughput -s [port]'
37+
print 'and then: (on host_B) throughput -c count host_A [port]'
38+
sys.exit(2)
39+
40+
41+
def server():
42+
if len(sys.argv) > 2:
43+
port = eval(sys.argv[2])
44+
else:
45+
port = MY_PORT
46+
s = socket(AF_INET, SOCK_STREAM)
47+
s.bind('', port)
48+
s.listen(0)
49+
print 'Server ready...'
50+
while 1:
51+
conn, (host, remoteport) = s.accept()
52+
while 1:
53+
data = conn.recv(BUFSIZE)
54+
if not data:
55+
break
56+
del data
57+
conn.send('OK\n')
58+
conn.close()
59+
print 'Done with', host, 'port', remoteport
60+
61+
62+
def client():
63+
if len(sys.argv) < 4:
64+
usage()
65+
count = int(eval(sys.argv[2]))
66+
host = sys.argv[3]
67+
if len(sys.argv) > 4:
68+
port = eval(sys.argv[4])
69+
else:
70+
port = MY_PORT
71+
testdata = 'x' * (BUFSIZE-1) + '\n'
72+
t1 = time.millitimer()
73+
s = socket(AF_INET, SOCK_STREAM)
74+
t2 = time.millitimer()
75+
s.connect(host, port)
76+
t3 = time.millitimer()
77+
i = 0
78+
while i < count:
79+
i = i+1
80+
s.send(testdata)
81+
s.shutdown(1) # Send EOF
82+
t4 = time.millitimer()
83+
data = s.recv(BUFSIZE)
84+
t5 = time.millitimer()
85+
print data
86+
print 'Raw timers:', t1, t2, t3, t4, t5
87+
print 'Intervals:', t2-t1, t3-t2, t4-t3, t5-t4
88+
print 'Total:', t5-t1
89+
print 'Throughput:', int(float(BUFSIZE*count) / float(t5-t1)),
90+
print 'K/sec.'
91+
92+
93+
main()

Demo/sockets/udpecho.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#! /usr/local/python
2+
3+
# Client and server for udp (datagram) echo.
4+
#
5+
# Usage: udpecho -s [port] (to start a server)
6+
# or: udpecho -c host [port] <file (client)
7+
8+
import sys
9+
from socket import *
10+
11+
ECHO_PORT = 50000 + 7
12+
BUFSIZE = 1024
13+
14+
def main():
15+
if len(sys.argv) < 2:
16+
usage()
17+
if sys.argv[1] = '-s':
18+
server()
19+
elif sys.argv[1] = '-c':
20+
client()
21+
else:
22+
usage()
23+
24+
def usage():
25+
sys.stdout = sys.stderr
26+
print 'Usage: udpecho -s [port] (server)'
27+
print 'or: udpecho -c host [port] <file (client)'
28+
sys.exit(2)
29+
30+
def server():
31+
if len(sys.argv) > 2:
32+
port = eval(sys.argv[2])
33+
else:
34+
port = ECHO_PORT
35+
s = socket(AF_INET, SOCK_DGRAM)
36+
s.bind('', port)
37+
print 'udp echo server ready'
38+
while 1:
39+
data, addr = s.recvfrom(BUFSIZE)
40+
print 'server received', `data`, 'from', `addr`
41+
s.sendto(data, addr)
42+
43+
def client():
44+
if len(sys.argv) < 3:
45+
usage()
46+
host = sys.argv[2]
47+
if len(sys.argv) > 3:
48+
port = eval(sys.argv[3])
49+
else:
50+
port = ECHO_PORT
51+
addr = host, port
52+
s = socket(AF_INET, SOCK_DGRAM)
53+
s.bind('', 0)
54+
print 'udp echo client ready, reading stdin'
55+
while 1:
56+
line = sys.stdin.readline()
57+
if not line:
58+
break
59+
s.sendto(line, addr)
60+
data, fromaddr = s.recvfrom(BUFSIZE)
61+
print 'client received', `data`, 'from', `fromaddr`
62+
63+
main()

0 commit comments

Comments
 (0)