-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test server to demo functionality
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) |