Hydra - Chat

Stato
Discussione chiusa ad ulteriori risposte.

HackLife

Utente Silver
26 Maggio 2008
57
11
0
73
Ecco un piccolissimo servizio di chat che ho scritto oggi, più che altro come esperimento per i socket e i thread. Nel pacchetto sono incusi due files, uno è l'ho e uno il client. Per far funzionare la chat, prima di tutto avviare il programma host e, se necessario, sbloccarlo dal firewall. Successivamente avviare il programma client su qualunque computer, digitando il proprio nickname e l'IP della macchina a cui ci si vuole connettere.
Se l'host non è attivato si otterrà un errore di connection refused.

Spero vi aiuti come farlo ha aiutato me ;)
hlife

Pare che gli allegati siano temporaneamente fuori uso. Clicca qui per il download

Codice:
Copyright (c) HackLife
CC - sa/by/nc
Codice:
#----- Hydra -----#
#Title:     Hydra - Host
#Author:    HackLife
#Descr:     A small application to start an Hydra chat
#Version:   2
#-----------------#
#Copyright: 2009 HackLife
#Mail:      [email protected]
#-----------------#


import threading
from socket import *

clients = []

class Host:
    def __init__(self):
        myHost = ''
        myPort = 4545S

        self.s = socket(AF_INET, SOCK_STREAM)
        self.s.bind((myHost, myPort))        
        self.s.listen(10)

    def run(self):
        
        while 1:            
            connection, address = self.s.accept()
            print 'New connection established'
            newRecv= recvThread(connection, address)
            clients.append(connection)
            newRecv.start()            

class recvThread(threading.Thread):
    def __init__(self, connection, address):
        self.connection = connection
        self.address = address
        
        threading.Thread.__init__(self)

    def run(self):
        while 1:
            try:
                data = self.connection.recv(1024)
            except:
                clients.remove(self.connection)
                for chan in clients:
                    chan.send('Connection Closed')
                    print 'Connection Lost' 
                    break
            if data:
                print data
                for chan in clients:
                    chan.send(data)
                    print 'Packet sent'

print """Welcome to Hydra. This is the hosting service.
Copyright (c) HackLife 2009

Waiting for incoming connections"""

mhost = Host()
mhost.run()
Codice:
#----- Hydra -----#
#Title:     Hydra - Client
#Author:    HackLife
#Descr:     A small application to connect to an Hydra Host
#Version:   2
#-----------------#
#Copyright: 2009 HackLife
#Mail:      [email protected]
#-----------------#

from socket import *
import threading

NICKNAME = '<%s>' %raw_input('Type in your nickname: ')
SERVER = raw_input('Type in the host you want to connect to')

serverHost = SERVER
serverPort = 2000

class sendThread(threading.Thread):
    def __init__(self, connection):
        self.connection = connection
        threading.Thread.__init__(self)

    def run(self):
        while 1:
            data = self.connection.recv(1024)
            if data:
                print data

print """Welcome to Hydra. This is client service.
Copyright (c) 2009 HackLife
You're logged as %s. Type -? for help.""" %NICKNAME

while 1:
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((serverHost, serverPort))
    sThread = sendThread(s)
    sThread.start()
    s.send(NICKNAME + ' joined the room')
    while 1:
        data = raw_input('')
        if data == '-?':
            print 'Available Commands: -? -n'
            break
        if data == '-n':
            onick = NICKNAME
            NICKNAME = '<%s>' %raw_input('Type your new nickname: ')
            s.send(onick +' is now known as '+NICKNAME)
        s.send(NICKNAME + data)
 
I miei compagni di classe me l'hanno già chiesta xD
Adesso volevo implementare qualche funzione tipo quelle per vedere chi c'è online e per kickare gli utenti indesiderati. Magari anche registrare i nickname sarebbe carino. La crittografia potrebbe essere utile?
 
Stato
Discussione chiusa ad ulteriori risposte.