سلام
دوستان میخوام یک برنامه چت در شبکه بنویسم ، [برای مشاهده لینک ها شما باید عضو سایت باشید ]
خوندم که به خواسته من خیل نزدیک بوده . حالا کد خودم را میزارم که در حد اینکه کلاینت میتونه به سرور با جی سون یک پیام را میفرستن
کد سرور :
کد:
import json
from twisted.inteet import protocol, reactor
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
# print (data)
parsed_json = json.loads(data)
print (parsed_json)
def coectionMade(self):
self.transport.write('okkkkkkkk')
class EchoFactory(protocol.Factory):
def buildProtocol(self, add):
retu Echo()
reactor.listenTCP(8000, EchoFactory())
reactor.run()
کد:
import json,datetime
msg=raw_input("messege")
json_string = '{"textmessege":"%s","id_sender":"%s","id_reciver":"%s","detail":"%s"}'%(msg,'1','2',datetime.datetime.now())
parsed_json = json.loads(json_string)
##################################################################################################################
############################### NETWORK ##############################################
##################################################################################################################
from twisted.inteet import protocol, reactor
class EchoClient(protocol.Protocol):
def coectionMade(self):
self.transport.write(json.dumps(parsed_json))
def dataReceived(self, data):
print 'server said: ', data
self.transport.loseCoection()
class EchoFactory(protocol.ClientFactory):
def buildProtocol(self, addr):
retu EchoClient()
def clientCoectionFailed(self, coector, reason):
print 'Coection Failed'
reactor.stop()
def clientCoectionLost(self, coector, reason):
print 'Coection lost'
reactor.stop()
reactor.coectTCP('localhost', 8000, EchoFactory())
reactor.run()
برچسب:
نویسنده: محمد رضا جوادیان