-
Notifications
You must be signed in to change notification settings - Fork 12
/
testserver.py
66 lines (46 loc) · 1.33 KB
/
testserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# Author: sc0tfree
# Twitter: @sc0tfree
# Email: [email protected]
import os
import socket
def generate_random_hex(length):
'''
Generates a hex string of arbitrary length - 1, ending in a newline.
'''
hex_string = os.urandom(length - 1)
hex_string += '\x0a'
return hex_string
host = '127.0.0.1'
port = 12345
s = socket.socket()
s.bind((host, port))
s.listen(5)
try:
while True:
c, addr = s.accept()
print 'Connection established from', addr[0], ':', addr[1]
c.send('Hello from Test Server\n')
# Echo Test
c.send('Echo Test - enter string:')
data = c.recv(1024)
print 'Echo Test - received: ', data
c.send('Echo Test - received: ' + data + '\n')
# Hex Test
c.send('Hex Test - enter length:')
data = c.recv(1024)
try:
hex_length = int(data)
except ValueError:
c.send('You must enter a number. Defaulting to 10.\n')
hex_length = 10
hex_string = generate_random_hex(hex_length)
c.send('Sending hex string...\n\n')
print 'Hex Test - sending: ', hex_string
c.send(hex_string)
c.close()
print 'Closed connection to ', addr[0], ':', addr[1]
except KeyboardInterrupt:
c.close()
print '\nExiting...'
exit(0)