FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Python Chat





Thundaa88
Actually, I have no idea of programming, but i tried to write a little chat with python:

Am I able to upload server.py on frihost and connect later to it from my home computer?

Code:




from time import *

def recv_only(conn,connlist,namen):
    data=[]
   
    while data!='exit':   
       
        data = conn.recv(1024)
                 
        if data!=[]:
           for i in range(len(connlist)):
               try:
                   connlist[i].send(data)
               except:
                   connlist.remove(connlist[i])
                   
                   
                   for t in range(len(namen)-1):
                       strnamen=namen[i]+' ist offline'
                       namen.remove(namen[i])
                       connlist[t].send(strnamen)
                       connlist[t].send(data)
                       #connlist[t].send(namen)
                       
                       
                       
                   break
                   
        # import winsound

        # winsound.Beep(350,500)
       
        namelist=','.join(namen)
        if len(namen) < 1:
            namelist='Es ist niemand online.'
        elif len(namen) == 1:
            namelist='Es ist online: '+namelist
        else:
            namelist='Es sind online: '+namelist   
       
        gesendet_5 = data[-5:]
        gesendet_6 = data[-6:]
        gesendet_7 = data[-7:]
        gesendet_10 = data[-10:]
       
        if gesendet_10 == '/datumzeit':
            print data
            lt = localtime()
            conn.send(strftime("Wir haben den %d.%m.%Y, es ist %H:%M:%S Uhr.", lt))
            print strftime("Wir haben den %d.%m.%Y, es ist %H:%M:%S Uhr.", lt)
        elif gesendet_7 == '/online':
            print data
            conn.send(namelist)
            print namelist
        elif gesendet_6 == '/datum':
            print data
            lt = localtime()
            conn.send(strftime("Wir haben den %d.%m.%Y.", lt))
            print strftime("Wir haben den %d.%m.%Y.", lt)
        elif gesendet_5 == '/zeit':
            print data
            lt = localtime()
            conn.send(strftime("Es ist %H:%M:%S Uhr.", lt))
            print strftime("Es ist %H:%M:%S Uhr.", lt)
        else:
            print data 
    return
   
   
def send_only(connlist):
    data=[]
   
    while data!='exit':
       
        data=raw_input()
        data_1=name+': '+data
       
       
        # now send
        #conn.send(data_1)
        for i in range(len(connlist)):
            try:
                connlist[i].send(data_1)
               
            except:
                pass
       
        print data_1
    return




def multiclient(s):
    data = []
    connlist=[]
    namen=[]
    while data != 'exit':
        s.listen(1) #busy waiting
        new_conn, new_addr = s.accept()
        print 'Connection accepted. Next Client is at: ', new_addr
        new_conn.send('Connection accepted!')
        new_conn.send('')
        namelist=','.join(namen)
        if len(namen) < 1:
            namelist='Es ist niemand online.'
            new_conn.send(namelist)
        elif len(namen) == 1:
            namelist='Es ist online: '+namelist
            new_conn.send(namelist)
        else:
            namelist='Es sind online:'+namelist
            new_conn.send(namelist)
        name=new_conn.recv(1024)
        new_name='New User online: '+name
        namen.append(name)
        #Start Thread recieve
        connlist.append(new_conn)
        for i in range(len(connlist)-1):
            try:
                connlist[i].send(new_name)
                #connlist[t].send(namen)
            except:
                pass
        thread.start_new_thread(recv_only,(new_conn,connlist,namen))
        #Start Thread send
        thread.start_new_thread(send_only,(connlist,))
       
           
       
    return new_conn





# simple illustration client/server pair; client program sends a string
# to server, which echoes it back to the client (in multiple copies),
# and the latter prints to the screen

# this is the server
#usage: server
import socket
import sys
import thread
print '###'
print ''
print 'Messenger Beta 0.4'
print ''
print '###'
print ''
print 'Server version 0.4'
print ''
name='server' #raw_input('Geben Sie Ihren Namen ein:')
 # create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# associate the socket with a port
host = '' #can leave this blank on the server side
port = 2000#int(sys.argv[1])
s.bind((host, port))
print 'Waiting.....'
#s.listen(1) #busy waiting
#conn, addr = s.accept()
#print 'Connection accepted. Client is at: ', addr
#conn.send('Connection accepted.')

thread.start_new_thread(multiclient,(s,))
# Start Thread recieve
#thread.start_new_thread(recv_only,(new_conn,))
#Start Thread send
#thread.start_new_thread(send_only,(new_conn,))




while True:
     pass
   
   
# close the connection
#new_conn.close()
Thundaa88
And here the compatible client!
Code:


import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

   

   

 


def recv_only(conn):
   
    data='blub'
    #while data!='exit':   
   
    data = conn.recv(1024)
       
       
       
               
    return data
   
   
def send_only(conn,data):
   
   
       
    data=name+' : '+data
   
       
       
        # now send
    conn.send(data)
        #
    return
     




# simple illustration client/server pair; client program sends a string
# to server, which echoes it back to the client (in multiple copies),
# and the latter prints to the screen

# this is the client
# usage: python client.py hostname  strToSend
import socket
import sys
import thread


print '###'
print ''
print 'Messenger Beta 0.2'
print ''
print '###'
print ''
print 'Client version 0.2'
 # create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
name='Erik' #raw_input('Geben Sie Ihren Namen ein:')
# connect to server
host = 'localhost'#sy
#s.argv[1] # server address
port = 2000#int(sys.argv[2]) # server port
s.connect((host, port))
#-----------------
#data = s.recv(1024)
#print data
#

s.send(name)

class Example(QtGui.QWidget):
     
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()
       
    def initUI(self):
       
        self.button = QtGui.QPushButton('Send', self)
        self.button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.textin = QtGui.QLineEdit(self)
        self.textin.setGeometry(130,200,200,50)
        self.button.move(20, 20)
       
        self.setFocus()
       
        self.label = QtGui.QTextBrowser(self)
        self.label2 = QtGui.QLabel(self)
        self.label.setGeometry(130,22,200,150)
        self.label2.setGeometry(350,-45,100,150)
        self.label2.setText('Online sind:')
        self.setWindowTitle('Messenger 0.5')
        self.setGeometry(300, 300, 500, 350)
       
   
       
        self.DataCollector=recv(self)
        self.connect(self.DataCollector,QtCore.SIGNAL("Activated ( QString ) "), self.Activated)
        self.DataCollector.start()
        #self.sendData=send(self)
       
        self.connect(self.button, QtCore.SIGNAL('clicked()'), self.sendData)
        self.connect(self.textin, QtCore.SIGNAL('returnPressed()'), self.sendData)
        #self.sendData.start()
       
      
    def Activated(self,newtext):
       
       
        self.label.insertPlainText(newtext)
        self.label.insertPlainText("\n")
        self.label.moveCursor(QtGui.QTextCursor.End)
        #self.textin.setFocus()
       
           
         
   
       
    def sendData(self):
       
        data=str(self.textin.text())
       
       
        send_only(s,data)
        self.textin.clear()
   

class recv(QtCore.QThread):
   def __init__(self,parent=None):
                 QtCore.QThread.__init__(self,parent)
                 self.text = ''
   def run(self):
                 while self.text != 'exit':
                     self.text = recv_only(s)
                     self.emit(QtCore.SIGNAL("Activated( QString )"),self.text)
                     

                     
if __name__ == '__main__':
       
       
         
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()       

 
   









while True:
     pass
     
s.close()

Thundaa88
Code:

def code(currenttext):
    liste=list(currenttext)
   
    code=''
    for i in range(len(liste)):
        newcode=ord(liste[i])
        if newcode%2==0:
            newcode=newcode+1
        else:
            newcode=newcode-1
        code=code+chr(newcode)
    print code
    return code
Thundaa88
Code:

def datei_op(currenttextfile,codint=1):

   
    data=open(currenttextfile,'r')
    text=data.read()
    if codint == 1:
        text=code(text)
    else:
        text=decode(text)
    data=open(currenttextfile,'w')
    data.write(text)   
    data.close()
    return text
   
def code(currenttext):
    liste=list(currenttext)
   
    code=''
    for i in range(len(liste)):
        newcode=ord(liste[i])
        if newcode%2==0:
            newcode=newcode+1
        else:
            newcode=newcode-1
        code=code+chr(newcode)
    print code
    return code   
   



def decode(currenttext):
    liste=list(currenttext)
       
    code=''
    for i in range(len(liste)):
        newcode=ord(liste[i])
        if newcode%2==0:
            newcode=newcode+1
        else:
            newcode=newcode-1
        code=code+chr(newcode)
    print code
    return code   


datei_op('example.txt',1)
Thundaa88
Code:

def sortsmart(liste):
   
   
   
    countit=range(len(liste))
    newliste=[]
    for i in countit:
       
       for t in (range(len(liste))):
           if liste[0]>=liste[t]:
           
               holdit=liste[t]
               
               liste[t]=liste[0]
               liste[0]=holdit
       newliste.append(liste[0])       
       liste.remove(liste[0])         
           
    return newliste                     
       
print sortsmart([2,5,4,7,8,6,5])   
Hello_World
You have no idea about programming?

I think you do lol.
Related topics
Chat script
Monty Python
Python Tutorial
Python
Description from general chat
123 Flash Chat
Rules for posting in the General Chat forum
Where I can find a 3d chat script
php chat
FriHost's official IRC Chat Channel
I cant install php chat tables nor set up diffrent subdomain
CHAT!
Php chat
PHP & Chat
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.