Skip to content

Instantly share code, notes, and snippets.

@marvin
Created December 14, 2012 16:28
Show Gist options
  • Save marvin/4286692 to your computer and use it in GitHub Desktop.
Save marvin/4286692 to your computer and use it in GitHub Desktop.
pygame client/server example
from socket import *
import time, random, thread
Port, Port2, HostIP, myname, mypw, newdata = 50005, 50003, "localhost", "", "", ""
gamemode = "lobby"
Clientsonline = []
def recvloop(soc):
global newdata
while 1:
newdata = soc.recv(1024)
newlist = newdata.split()
newjob = newlist[0]
newlist.pop(0)
if newjob == "say":
newname = newlist[0]
newlist.pop(0)
newlist2 = " ".join(newlist)
print newname, newjob, newlist2
if newjob == "upd":
newlist2 = newlist[0]
Clientsonline = newlist2.split()
print "Clients online: ", Clientsonline
def sendloop(soc2):
while 1:
inputtxt = raw_input("")
inputtxt = "say %s" %inputtxt
soc2.send(inputtxt)
soc = socket(AF_INET, SOCK_STREAM)
soc.connect((HostIP, Port))
while 1:
print "only 1 word is taken for each name and password"
myname = raw_input("Your name please: ")
myname = myname.split()
myname = myname[0]
mypw = raw_input("Your password? ")
mypw = mypw.split()
mypw = mypw[0]
soc.send("%s %s" %(myname, mypw))
incoming = soc.recv(1024)
if incoming == "welcome":
print "welcome, you are online now"
break
if incoming == "wrong password":
print "wrong password or name is taken already"
soc2 = socket(AF_INET, SOCK_STREAM)
soc2.connect((HostIP, Port2))
thread.start_new_thread(recvloop, (soc, ))
thread.start_new_thread(sendloop, (soc2, ))
while 1:
pass
from socket import *
import time, thread, random
Clients, Clientsonline, Port, Port2, dummy, HostIP = {}, [], 50005, 50003, "", ""
Outtube, Intube, Worktube = [], [], []
###################################### DATABASE ################################
def writedatabase():
try:
f = open("database", "r+")
except:
f = open("database", "w")
f.close()
print "database created"
f = open("database", "r+")
for element in Clients:
savestring = ""
for item in Clients[element][0]:
savestring = "%s%s " % (savestring, item)
savestring = "%s\n" % savestring
f.write(savestring)
f.close()
print "database saved"
def readdatabase():
try:
f = open("database", "r+")
for line in f:
g = line.split()
name = g[0]
Clients[name] = [[name], ["conn", "addr"], ["conn2", "addr2"]]
g.pop(0)
for element in g:
Clients[name][0].append(element)
except:
print "couldnt read database"
def saveloop(dummy):
while 1:
time.sleep(120)
writedatabase()
####################################### COMMUNICATION ##########################
def acceptconthread(dummy):
soc = socket(AF_INET, SOCK_STREAM)
soc.bind((HostIP, Port))
soc.listen(100)
while 1:
conn, addr = soc.accept()
thread.start_new_thread(passwordthread, (conn, addr))
def passwordthread(conn, addr):
while 1:
name = conn.recv(1024)
name2 = name.split()
name, password = name2[0], name2[1]
aaa = name in Clients
if aaa == True:
if password == Clients[name][0][1]:
Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
print "%s connected with password %s" %(name, password)
conn.send("welcome")
Clientsonline.append(name)
Worktube.append([name, "upd"])
break
if password != Clients[name][0][1]:
conn.send("wrong password")
if aaa == False:
Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
print "%s connected with password %s" %(name, password)
conn.send("welcome")
Clientsonline.append(name)
Worktube.append([name, "upd"])
break
soc2 = socket(AF_INET, SOCK_STREAM)
soc2.bind((HostIP, Port2))
soc2.listen(100)
conn2, addr2 = soc2.accept()
Clients[name][2]=[conn2, addr2]
thread.start_new_thread(sendloop, (name, conn, addr))
thread.start_new_thread(recvloop, (name, conn2, addr2))
def sendloop(name, conn, addr):
try:
while 1:
if len(Outtube) > 0:
if Outtube[0][0] == name:
sendstring = "%s %s" % (Outtube[0][1], Outtube[0][2])
conn.send(sendstring)
Outtube.pop(0)
except:
bbb = Clientsonline.remove(name)
def recvloop(name, conn2, addr2):
try:
while 1:
data = conn2.recv(1024)
data2 = data.split()
jobname = data2[0]
data2.pop(0)
Intube.append([name, jobname, data2])
except:
print name, "has disconnected"
def workthrough():
while 1:
if len(Worktube) > 0:
if Worktube[0][1] == "upd":
newlist = Clientsonline[:]
newlist2 = " ".join(newlist)
Outtube.append([Worktube[0][0], "upd", newlist2])
Worktube.pop(0)
if len(Worktube) > 0:
if Worktube[0][1] == "say":
Worktube[0][2] = "%s %s" % (Worktube[0][0], Worktube[0][2])
for client in Clientsonline:
Outtube.append([client, "say", Worktube[0][2]])
Worktube.pop(0)
if len(Intube) > 0:
if Intube[0][1] == "say":
naostring = " ".join(Intube[0][2])
Worktube.append([Intube[0][0], "say", naostring])
Intube.pop(0)
#################################### ACTION ####################################
readdatabase()
thread.start_new_thread(acceptconthread, (dummy, ))
thread.start_new_thread(saveloop, (dummy, ))
####thread.start_new_thread(workthrough, (dummy, ))
while 1:
workthrough()
@akbarnotopb
Copy link

i dont see any pygame here ._.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment