-
Notifications
You must be signed in to change notification settings - Fork 1
/
SSLserverPi_V1_3.py
768 lines (684 loc) · 26.6 KB
/
SSLserverPi_V1_3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#####################################################
# Copyright (C) 2016
# IoT4pi <office@iot4pi.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Apache License Version 2.0 (APLv2)
# as published by http://www.apache.org/licenses/LICENSE-2.0 .
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# This product includes software developed by the OpenSSL Project
# for use in the OpenSSL Toolkit. (http://www.openssl.org/)
#
#####################################################
# ! /usr/bin/env python
import socket
import sys
import os
# import thread
import random
import subprocess
from subprocess import PIPE, Popen
from Queue import Queue, Empty
import threading
from threading import Thread
import time
import datetime
# import copy
from ConfigParser import SafeConfigParser
try:
import ssl
except ImportError:
print 'Error importing SSL !!!'
sys.exit()
##########################
# Config variables
bUseConfig = True
strConfig = 'SSLPi.cfg'
iPort = 10023
strHost = ''
strPath = ''
strCert = 'server.pem'
strKey = 'server.key'
strEndStat = 'EndCom'
bDebug = True
bDetailError = True
cmdList = [] # storage of all commands
cmdParam = [] # list of just the commands that can come with parameter
maxThreads = 10
#####################
###############################
# Class for Running the Command asynchron
# this will work only reading what the Server is sending
# there is only limeted why to send Data from client tothe server
class myThread(threading.Thread, ):
BUFSIZE = 1024
def __init__(self, connstream, cmd):
threading.Thread.__init__(self)
self.connstream = connstream
# self.stdout = None
# self.stderr = None
self.cmd = cmd
self.isrunning = True
self.PID = None
def run(self):
print "Starting asynchrone Thread with Command " + self.cmd
p = Popen([self.cmd], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
if bDebug:
print getTime() + ": PID of Thread = " + str(p.pid)
# wrap p.stdout with a NonBlockingStreamReader object:
nbsr = NonBlockingStreamReader(p.stdout)
# get the output
strTemp = ''
while self.isrunning:
output = nbsr.readline(0.1)
# 0.1 secs to let the shell output the result
if not output:
if strTemp != '':
# print strTemp
try:
self.connstream.write(strTemp)
except:
if bDebug:
print getTime() + ": Connection was destroyed by Client!"
print getTime() + ": PID = " + str(os.getpid())
print getTime() + ": Group PID = " + str(os.getpgid(os.getpid()))
# p.kill() stops subprocess
p.kill()
self.isrunning = False
break
strTemp = ''
continue
else:
strTemp = strTemp + output
nbsr.stop()
return
# Get lock to synchronize threads
# threadLock.acquire()
def stop(self):
self.isrunning = False
print getTime() + ": Stopping " + self.name + ' sent !'
####End Class
######################
# Helper Class for non blocking
# reading from a subprocess output stream
class NonBlockingStreamReader:
def __init__(self, stream):
'''
stream: the stream to read from.
Usually a process' stdout or stderr.
'''
# self._t = None
self._s = stream
self._q = Queue()
self._isrunning = True
def _populateQueue(stream, queue):
'''
Collect lines from 'stream' and put them in 'quque'.
'''
while self._isrunning:
# readline doesn't work
# line = stream.readline()
line = stream.read(1)
if line:
queue.put(line, False)
# print 'Add Line to Queue...' + line
else:
print getTime() + ': UnexpectedEndOfStream !!!'
raise UnexpectedEndOfStream
if bDebug:
print getTime() + ": populate Queue Ended !"
return
self._t = Thread(target=_populateQueue,
args=(self._s, self._q))
self._t.daemon = True
self._t.start() # start collecting lines from the stream
def readline(self, timeout=None):
# print 'in readline..'
try:
return self._q.get(block=timeout is not None, timeout=timeout)
except Empty:
return None
def stop(self):
self._isrunning = False
if bDebug:
print getTime() + ": Stopping NonBlockingStreamReader !"
class UnexpectedEndOfStream(Exception): pass
###End Helper Class
#######################################
# Helper function
# Function to get present Time as String
def getTime():
ts = time.time()
strTime = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
return strTime
###### End Function
#########################################
# Function reading and parsing Config File
def ReadInConfig():
# store in global Variables
# therefore call explicit the global Keyword
global bDebug
global iPort
global strHost
global strPath
global strCert
global strKey
global strEndStat
global cmdList
global cmdParam
global bDetailError
# open parser file
config = SafeConfigParser()
try:
config.read(strPath + strConfig)
except:
if bDebug:
print getTime() + ": Error open config file !"
return False
################
# parse SETUP Variales
try:
if (config.get("SETUP", "bDebug") == 'True'):
bDebug = True
else:
bDebug = False
if (config.get("SETUP", "bDetailError") == 'True'):
bDetailError = True
else:
bDetailError = False
iPort = int(config.get("SETUP", "iPort"))
strHost = config.get("SETUP", "strHost")
strPath = config.get("SETUP", "strPath")
# if strPath not set we assume
# Cert Files are in the working Dir
if strPath == '':
strPath = os.getcwd() + '/'
strCert = config.get("SETUP", "strCert")
strKey = config.get("SETUP", "strKey")
strEndStat = config.get("SETUP", "strEndStat")
if bDebug:
print getTime() + ": strConfig = " + strConfig
print getTime() + ": iPort = " + str(iPort)
print getTime() + ": strHost = " + strHost
print getTime() + ": strPath = " + strPath
print getTime() + ": strCert= " + strCert
print getTime() + ": strKey= " + strKey
print getTime() + ": strEndStat= " + strEndStat
print getTime() + ": bDebug = " + str(bDebug)
print getTime() + ": bDetailError = " + str(bDetailError)
except:
if bDebug:
print getTime() + ": Error reading SETUP Variable from Config File !"
return False
###################################
# Read in the commands
lSections = config.sections()
if bDebug:
print 'Sections = '
print lSections
# Loop through Sections
for section in lSections:
if (section == 'APP_CALLS'):
# store Section APP_CALLS in lCommads List
lComands = config.options(section)
# read every line of the lCommads List
for line in lComands:
resLine = config.get(section, line)
# split the line with sperator ;
LineParts = resLine.split(";")
# store all commands in List
cmdList.append(LineParts)
if bDebug:
print 'Seperated Commandline ='
print LineParts
# if there is CommandIn which comes with parameter
# store it in the cmdParam List
if (LineParts[4] == '4') or (LineParts[4] == '5') or (LineParts[4] == '6'):
print 'Found Parameter command with ' + LineParts[0]
# but first proof if it isnot already in there
if LineParts[0] not in cmdParam:
cmdParam.append(LineParts[0])
return True
###### End Function
#########################################
# Function Processing the commands
# Test Command -- Value 0
# Change Dir -- Value 1
# Command without a return (like rm ) -- Value 2
# Command with return (like ls or pwd) -- Value 3
#
# Command In comes with parameters, with return -- Value 4
# Command In comes with parameters, without return -- Value 5
# Command In for Change Dir -- Value 6
########################################################################
# Run Command asynchron (as a Service) -- Value 7
#
# Function returns a Truple, iReturn as Integer with Internal(-3), Sucess (1), undefined (-2) or Error (0) and
# the String which can be either an Error description or the System feedback
#
def processCommand(connstream, cmdList, strParam=''):
iReturn = -3
strReturn = ''
for cmdLine in cmdList:
# print '++++++++++++++++++++'
# print cmdLine
###########
# TestCommand
if cmdLine[4] == '0':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Test Command'
iReturn = 1
strReturn = strReturn + "Test message fom SSL server\r\n"
#########
# chDir
elif cmdLine[4] == '1':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Change Dir'
try:
# change Dir returns None in any case
# if it fails there will be an exception
os.chdir(cmdLine[6])
iReturn = 1
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Change Dir did not work'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' Change Dir did not work\r\n'
################
# Command without a return
elif cmdLine[4] == '2':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command without a return'
# print 'CommandIn ' +cmdLine[0]
# print 'Command ' +cmdLine[3]
# print 'Parameter ' +cmdLine[6]
# print 'Return ' +cmdLine[5]
try:
strTemReturn = subprocess.call([cmdLine[3], cmdLine[6]])
# proof for success
if strTemReturn == int(cmdLine[5]):
iReturn = 1
else:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
#############
# Command with return
elif cmdLine[4] == '3':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command with return'
# print cmdLine[0]
# print cmdLine[6]
try:
# proof if there has to be an argument to be included
if (len(cmdLine) < 7) or (cmdLine[6] == ''):
strTempReturn = subprocess.check_output([cmdLine[3]])
else:
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# The application could be extended reading more the
# then one argument.
# Therefore I would change passing a List of arguments instead of
# the individual arguments.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
strTempReturn = subprocess.check_output([cmdLine[3], cmdLine[6]])
iReturn = 1
strReturn = strReturn + strTempReturn + '\r\n'
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' Command with return did not work'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' Command with return did not work\r\n'
#########
# CommandIn comes with parameters, with return
elif cmdLine[4] == '4':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command In comes with parameters, with return'
try:
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# The application could be extended reading more the
# then one argument.
# Therefore I would change passing a List of arguments instead of
# the individual argument strParam.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
strTempReturn = subprocess.check_output([cmdLine[3], strParam.strip()])
iReturn = 1
strReturn = strReturn + strTempReturn + '\r\n'
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' Command In with return did not work'
iReturn = 0
trReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' Command In with return did not work\r\n'
########
# CommandIn comes with parameters, without return
elif cmdLine[4] == '5':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command In comes with parameters, without return'
try:
strTemReturn = subprocess.call([cmdLine[3], strParam.strip()])
# print 'Return of CommandIn ='
# print 'strTemReturn = ' + str(strTemReturn)
# print 'cmdLine[5] =' + cmdLine[5]
# proof for success
if strTemReturn == int(cmdLine[5]):
iReturn = 1
else:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' ' + cmdLine[3] + ' did not work\r\n'
########
# CommandIn for Change Directory chdir
elif cmdLine[4] == '6':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command In for Change Dir'
try:
# change Dir returns None in any case
# if it fails there will be an exception
os.chdir(strParam.strip())
iReturn = 1
except:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command In for Change Dir did not work'
iReturn = 0
strReturn = strReturn + cmdLine[0] + ' Command In for Change Dir did not work\r\n'
##################################
########
# Run Command asynchron (as a Service)
elif cmdLine[4] == '7':
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Run Command asynchron'
########
# Create new threads
thread1 = myThread(connstream, cmdLine[3])
print getTime() + ': threat should be created'
# Start new Threads
thread1.start()
# thread1.join()
print getTime() + ': Main thread again ...'
# print thread1.stdout
##############################
#############
# Command Type not defined
else:
if bDebug:
print getTime() + ': ' + cmdLine[0] + ' Command Type not defined'
iReturn = -2
# !!!!!!!!
# Stop Loop and exit function
# if it has not been successful
if iReturn != 1:
break
return (iReturn, strReturn)
###### End Function
#########################################
# Function for implementing own code
# return codes
# -2 Command not Found
# -1 Client ends communication
# 0 Error occoured
# 1 Success
# 2 unknown
#
def Proceed_func(fromaddr, data, connstream):
# will hold the relevant commands
passCommand = []
# holds parameter if there will be one
strSecond = ''
strFirst = ''
if bDebug:
print getTime() + " :from IP :" + str(fromaddr), data
# proof if Client end communication
if (data == strEndStat):
print getTime() + ': Client quits'
return -1
# is the received message one with Parameter ?
for strParam in cmdParam:
if (data.startswith(strParam)):
# if yes extract Parameter
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# The application could be extended reading more the
# then one argument.
# Therefore I would change passing a List of arguments instead of
# the variable strSecond.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
strSecond = data[len(strParam):]
strFirst = strParam
if bDebug:
print getTime() + ': ' + data + ' is in cmdParam'
print getTime() + ': Parameter = ' + strSecond
print getTime() + ': Command = ' + strFirst
# if there are no Parameter leave the
# function withh erorr message
if len(strSecond.strip()) < 1:
if bDebug:
print getTime() + ': ' + data + ' is in cmdParam with missing Parameter : ERROR'
connstream.write('ERROR\r\n' + data + ' is in cmdParam with missing Parameter' + '\r\n')
return 0
# now run through command List to
# do the sequence
for strComandLine in cmdList:
if (strFirst == strComandLine[0]):
# print strComandLine
passCommand.append(strComandLine)
# is the received Message one without Parameter ?
# print '#####################'
for strComandLine in cmdList:
if (data == strComandLine[0]):
# print strComandLine
passCommand.append(strComandLine)
# If message is relevant, process the commands
if not passCommand:
return -2
else:
iCode, strBack = processCommand(connstream, passCommand, strSecond)
# print 'return Code = ' + str(iCode)
# print 'return String = ' + strBack
# SUCCESS
if (iCode == 1):
if strBack != '':
connstream.write('OK\r\n' + strBack)
else:
connstream.write('OK\r\n')
return 1
# ERROR
elif (iCode == 0):
if bDetailError == True:
connstream.write('ERROR\r\n' + strBack)
else:
connstream.write('ERROR\r\n')
return 0
return 2
###### End function
###############################
# Socket Thread
# TODO:
# 1)by terminating the Thread there also has to be terminated the TCP Socket !!
#
# Creation of Threading Lock
threadLock = threading.Lock()
class mySSLThread(threading.Thread, ):
def __init__(self, threadID, newsocket, ip, port, context):
threading.Thread.__init__(self)
self.threadID = threadID
self.Mysocket = newsocket
self.ip = ip
self.port = port
self.isrunning = True
self.context = context
def run(self):
if bDebug:
print getTime() + ": Starting Thread No " + str(self.threadID)
try:
# Timout to give time to setup
time.sleep(2)
connstream = None
iLoop = 1
if bDebug:
print getTime() + ': certfile = ' + strPath + strCert
print getTime() + ': keyfile = ' + strPath + strKey
print getTime() + ': Working Dir = ' + os.getcwd()
connstream = self.context.wrap_socket(self.Mysocket, server_side=True)
##Code form Version Python 2.7.3
# establish a SSL Socket with client
# connstream = ssl.wrap_socket(self.Mysocket,
# server_side=True,
# certfile=strPath + strCert,
# keyfile=strPath + strKey)
if bDebug:
print getTime() + ': Thread: Accepted SSL connection to address ' + self.ip + ":" + str(
self.port) + "\n"
except ssl.SSLError, msg:
if bDebug:
print getTime() + ': ERROR Thread: Error. SSLError in Thread : ', msg[1]
if connstream != None:
print getTime() + ': ERROR SSLsocket could be created !'
else:
print getTime() + ': ERROR SSLsocket is NULL !'
iLoop = 0
# Loop for receiving the messages from the client
iReturn = 0
while iLoop:
data = connstream.recv(1280)
if not data:
continue
else:
# Block Threads
threadLock.acquire()
iReturn = Proceed_func((self.ip, self.port), data, connstream)
# Release Threads
threadLock.release()
if bDebug:
print getTime() + ': iReturn = ' + str(iReturn)
###############
# handling feedback from the message
# get of loop, if client quits
if (iReturn == -1):
connstream.write('Goobye\r')
break
# Message could not be found
if (iReturn == -2):
connstream.write('Message is not a Command\r')
# Sucess
elif (iReturn == 1):
pass
# ERROR
elif (iReturn == 0):
pass
else:
connstream.write('unknown\r')
if connstream != None:
# houskeeping, clean up SSL socket
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
self.stop()
def stop(self):
self.Mysocket.close()
if bDebug:
print getTime() + ':Thread: Goodbye !'
self.isrunning = False
if bDebug:
print getTime() + ": Stopping Thread No " + str(self.threadID)
# def __del__(self):
# print "Stopping " + self.name
###### End Thread
###############################################
# main() Function
def main():
err = 0
msg = None
ThreadNum = 1 # threadCounter
Threads = [] # ThredsList
#######
# Read in the Config if set
if bUseConfig:
if (ReadInConfig() == False):
if bDebug:
print getTime() + ": Error reading Config; Server quits"
err = 1
####################
# proof if certificates are availible
if os.path.exists(strPath + strCert):
if bDebug:
print getTime() + ': certificate file could be loaded'
else:
if bDebug:
print getTime() + ': certificate file does not exist'
err = 1
##New Code with Version Python 2.7.9
# it is recommended to use PROTOCOL_SSLv23 as the protocol version
# and then disable SSLv2 and SSLv3 explicitly using the SSLContext.options attribute
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
context.load_cert_chain(certfile=strPath + strCert, keyfile=strPath + strKey)
# establish a TCP socket
# bindsocket = socket.socket()
bindsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
bindsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
bindsocket.bind((strHost, iPort))
except socket.error, msg:
if bDebug:
print getTime() + ": Bind failed in server: " + str(msg[0]) + " Message " + msg[1]
err = 1
# loop , waiting for a TCP Connection and making a client thread if there is a connection
while (err == 0):
try:
try:
bindsocket.listen(4)
except socket.error, msg:
if bDebug:
print getTime() + ": Listen failed: " + str(msg[0]) + " Message " + msg[1]
err = 1
if bDebug:
print getTime() + ":Server is listening for connections\n"
(newsocket, (ip, port)) = bindsocket.accept()
if bDebug:
print getTime() + ":Accepted client connection to address " + ip + ":" + str(port) + "\n"
# start a new thread with TCP connection
# thread.start_new_thread(deal_with_client,(newsocket, fromaddr))
########
# Create new threads
if maxThreads >= threading.activeCount():
thread1 = mySSLThread(ThreadNum, newsocket, ip, port, context)
Threads.append(thread1)
if bDebug:
print getTime() + ":Threat No. " + str(ThreadNum) + " should be created \n"
print getTime() + ': Number of Threads = ' + str(threading.activeCount())
# Start new Threads
thread1.start()
ThreadNum = ThreadNum + 1
else:
if bDebug:
print getTime() + ': Number of Threads exceeded ! Sorry!'
except ssl.SSLError:
if bDebug:
print getTime() + ': Error: ssl.SSLError!'
err = 1
except:
if bDebug:
print getTime() + ": Unexpected error in While:", sys.exc_info()[0]
err = 1
# housekeeping, close TCP socket
bindsocket.close()
###### End main()function
#######################
main()
#######################