apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Telnet chat username and password

Sun Jan 05, 2014 11:54 pm

Is there someone could just simply just add a spot for a username and password so i can control who gets on my telnet chat heres the code.

Code: Select all

import socket, threading

HOST = '192.168.0.141'
PORT = 51234 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)
clients = [] 
lock = threading.Lock()


class chatServer(threading.Thread):
    def __init__(self, (socket,address)):
        threading.Thread.__init__(self)
        self.socket = socket
        self.address= address

    def run(self):
        lock.acquire()
        clients.append(self)
        lock.release()
        print '%s:%s connected.' % self.address
        while True:
            data = self.socket.recv(1024)
            if not data:
                break
            for c in clients:
                c.socket.send(data)
        self.socket.close()
        print '%s:%s disconnected.' % self.address
        lock.acquire()
        clients.remove(self)
        lock.release()

while True: 
    
    chatServer(s.accept()).start()
thanks

User avatar
DougieLawson
Posts: 35815
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Telnet chat username and password

Mon Jan 06, 2014 12:15 am

Code: Select all

        while True:
            data = self.socket.recv(1024)
            if not data:
                break
            for c in clients:
                c.socket.send(data)
Before you start that loop you'll need to send a password challenge and compare the return data against your password list. NOTE: There is NO security here, it's only an example.

Code: Select all

        self.socket.send("Enter userid/password")
        user_pw = self.socket.recv(1024)
# You will want to improve this part
# you may want to do two interactions one for userid, one for password
# you may want to count failures and drop the session after three
        if user_pw == "foo/bar" :
          while True:
              data = self.socket.recv(1024)
              if not data:
                  break
              for c in clients:
                  c.socket.send(data)
# If the user/pwd doesn't match drop through to the socket.close
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

apples723
Posts: 80
Joined: Sat Jun 22, 2013 3:13 pm

Re: Telnet chat username and password

Mon Jan 06, 2014 1:07 am

Could you post a complete code cause i keep getting syntx erros.

User avatar
DougieLawson
Posts: 35815
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Telnet chat username and password

Mon Jan 06, 2014 8:30 pm

Here's a debugged version.

Code: Select all

import socket, threading

HOST = socket.gethostname()
PORT = 51234

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)
clients = []
lock = threading.Lock()

class chatServer(threading.Thread):
        def __init__(self, (socket,address)):
            threading.Thread.__init__(self)
            self.socket = socket
            self.address= address

        def run(self):
            lock.acquire()
            clients.append(self)
            lock.release()
            print '%s:%s connected.' % self.address
# You WILL want to improve this part
# You may want to do two interactions one for userid, one for password
# You may want to count failures and drop the session after three failures
            self.socket.send("Enter userid/password\r\n")
            user_pw = self.socket.recv(1024)
# debugging pwstr = ":".join("{0:x}".format(ord(c)) for c in user_pw)
# debugging print user_pw , pwstr
            if user_pw[:7] == "foo/bar":
              while True:
                data = self.socket.recv(1024)
                if not data:
                    break
                for c in clients:
                    c.socket.send(data)
# If the user/pwd doesn't match drop through to the socket.close
            self.socket.close()
            print '%s:%s disconnected.' % self.address
            lock.acquire()
            clients.remove(self)
            lock.release()

while True:
        chatServer(s.accept()).start()
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Python”