I am working on a simple console chat in Python. I started from this Winforms application, removed all the forms (so that the chat could run on Windows and Linux) and did some refactoring (the "click events" did not exist any more, it needed a workaround).
The new client code:
import thread
from MinimalNetworkTools import *
HOST = str(get_internal_ip())
PORT = 8011
s = socket(AF_INET, SOCK_STREAM)
buffered_messages = ''
def UploadData():
global s
print 'listening for your input'
while 1:
buffered_messages = raw_input()
FlushMessages(s,buffered_messages)
def ReceiveData():
global s
thread.start_new_thread(UploadData,())
try:
s.connect((HOST, PORT))
LoadConnectionInfo('[ Succesfully connected ]\n---------------------------------------------------------------')
except:
LoadConnectionInfo('[ Unable to connect ]')
return
while 1:
try:
data = s.recv(1024)
except:
LoadConnectionInfo('\n [ Your partner has disconnected ] \n')
break
if data != '':
LoadOtherEntry(data)
else:
LoadConnectionInfo('\n [ Your partner has disconnected ] \n')
break
thread.start_new_thread(ReceiveData,())
while 1:
pass
The new server code:
import thread
from MinimalNetworkTools import *
s = socket(AF_INET, SOCK_STREAM)
HOST = gethostname()
PORT = 8011
conn = ''
s.bind((HOST, PORT))
def UploadData():
global conn
print 'listening for your input'
while 1:
buffered_messages = raw_input()
FlushMessages(conn,buffered_messages)
def GetConnected():
s.listen(1)
global conn
print 'Waiting for connection...'
conn, addr = s.accept()
LoadConnectionInfo('Connected with: ' + str(addr) + '\n---------------------------------------------------------------')
thread.start_new_thread(UploadData,())
while 1:
try:
data = conn.recv(1024)
LoadOtherEntry(data)
except:
LoadConnectionInfo('\n [ Your partner has disconnected ]\n [ Waiting for him to connect..] \n ')
GetConnected()
conn.close()
thread.start_new_thread(GetConnected,())
while 1:
pass
And here are the methods shared by both codes:
from Tkinter import *
from socket import *
import urllib
import re
def get_external_ip():
site = urllib.urlopen("http://checkip.dyndns.org/").read()
grab = re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', site)
address = grab[0]
return address
def get_internal_ip():
return str(gethostbyname(getfqdn()))
def FilteredMessage(EntryText):
EndFiltered = ''
for i in range(len(EntryText)-1,-1,-1):
if EntryText[i]!='\n':
EndFiltered = EntryText[0:i+1]
break
for i in range(0,len(EndFiltered), 1):
if EndFiltered[i] != "\n":
return EndFiltered[i:]+'\n'
return ''
def LoadConnectionInfo(EntryText):
if EntryText != '':
print 'Connection info : ' + EntryText
def LoadMyEntry(EntryText):
if EntryText != '':
print '\t\t -> Sent!'
def LoadOtherEntry(EntryText):
if EntryText != '':
print 'Other : ' + EntryText,
def FlushMessages(s,buffered_messages):
if len(buffered_messages) > 0 :
EntryText = FilteredMessage(buffered_messages)
LoadMyEntry(EntryText)
s.sendall(EntryText)
My questions are:
- How can I make it cleaner?
- how bad is the thread safety?