Skip to content

Commit

Permalink
Add test server to demo functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0tfree committed Jun 18, 2017
1 parent e123f31 commit b7cf20c
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions testserver.py
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)

0 comments on commit b7cf20c

Please sign in to comment.