shadowbyte
Posts: 18
Joined: Sat Apr 09, 2016 3:29 am

Need help with simple BBS/telnet server code.

Wed Mar 15, 2017 3:18 am

The following code works fine the first time I make a connection, then crashes on the second connection.

Can someone help me understand my implementation a little better?

My current code looks like this..

Code: Select all

#!/usr/bin/python

import socket
import sys
import time
from thread import *

HOST  = '192.168.1.18' # ip
PORT  = 64 # port
ascii = 0
ansi  = 1
petscii = 2

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#  Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Waiting for caller...'

#Main Bulletin Board System Function
def clientthread(conn):
    type = ascii

    cr()
    cr()
    cr()
    cr()
    output('Welcome to the BBS',type)
    cr()
    cr()
    file('welcome.txt',type)
    cr()
    output('Login:',type)
    input(type)
    output('Password:',type)
    input(type)
    ## Disconnect from the BBS
    cr()
    cr()
    output('Thank you for calling! Goodbye! <CLICK!>',type)
    #came out of loop
    conn.close()
    s.close()
#Function: Input from caller
def input(screentype):
    while True:
        #Receiving from client
        if screentype == petscii:
           data = conn.recv(1) #1024
        if screentype == ascii:
           data = conn.recv(1024)
        if not data: break
        sys.stdout.write(data)
        sys.stdout.flush()
        if screentype == petscii:
           if ord(data) == 13:
              conn.send(chr(13))
              break
           print ord(data)
        conn.sendall(data)
#Function: Output to screen
def output(str,screentype):
    if screentype == ascii:
        for letter in str:
            conn.send(letter)
            sys.stdout.write(letter) #display to local terminal
            sys.stdout.flush()
            time.sleep(.01)
    if screentype == petscii:
        sentence = str.swapcase()
        for letter in sentence:
            conn.send(letter)
            sys.stdout.write(letter.swapcase()) #display to local terminal
            sys.stdout.flush()
            time.sleep(.01)
#Function: Display carriage return
def cr():
    conn.send(chr(13))
    sys.stdout.write('\n')
    sys.stdout.flush()
#Function: Display File to screen
def file(filename,screentype):
    f = open (filename,'r')
    for line in f:
        output(line,screentype)
        conn.send(chr(13))
    f.close()
    conn.send(chr(13))

#now keep talking with the client
while 1:
    STDERR = sys.stderr
    def excepthook(*args):
      print >> STDERR, 'caught'
      print >> STDERR, args
    sys.excepthook = excepthook



    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #start new thread takes 1st argument as a function name to be run,
    #second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))

s.close()

User avatar
paddyg
Posts: 2464
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Need help with simple BBS/telnet server code.

Wed Mar 15, 2017 8:05 pm

I would try putting my while loop inside a
try:
except KeyboardInterrupt:
structure so that the s.close() happened if I stopped the program by breaking out (there doesn't seem to be any other way) otherwise this code in unreachable.
PS I might actually make the stopping of the threads happen as well. This is best done by having an addition argument passed to the threads (then passed on to input() critically) that controls the while loop. However it would probably be easier to use a global

Code: Select all

def input(screentype):
    global keep_alive
    while keep_alive:
....
except KeyboardInterrup:
  keep_alive = False
  s.close()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

wartin
Posts: 1
Joined: Tue Apr 03, 2018 2:32 pm

Re: Need help with simple BBS/telnet server code.

Tue Apr 03, 2018 2:35 pm

shadowbite
Did you get any luck with your code ..... i would be interested in getting to look at it...
I would love to have a petscii bbs server

Return to “Python”