-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilbis-sunucu.py
174 lines (145 loc) · 4.1 KB
/
milbis-sunucu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import pkgutil
if pkgutil.find_loader("gsocketpool") is None:
os.system("pip3 install gsocketpool")
import gsocketpool.pool
if pkgutil.find_loader("mprpc") is None:
os.system("pip3 install mprpc")
if pkgutil.find_loader("gevent") is None:
os.system("pip3 install gevent")
from gevent.server import StreamServer
from mprpc import RPCServer
from time import gmtime, strftime
from sqlite3 import *
import hashlib
import os
import sys
SIFRE="Sqe4F5GSDax*-5"
VT_ADI="./sunucu.db"
MESAJ_DIZINI="./smesajlar/"
os.makedirs(MESAJ_DIZINI, exist_ok=True)
def fliste_icerik(fliste):
icerik=[]
for dos in fliste:
veri=[]
if os.path.exists(MESAJ_DIZINI+dos):
veri.append(dos)
veri.append(open(MESAJ_DIZINI+dos).read())
icerik.append(veri)
return icerik
def gliste_al(mliste):
sliste=[]
fliste=[]
for mesajd in os.listdir(MESAJ_DIZINI):
dosya=os.path.basename(mesajd)
sliste.append(dosya)
fliste=tuple(set(mliste) ^ set(sliste))
#print ("mesaj fark listesi >>> ",fliste)
return fliste_icerik(fliste)
def yeni_mesaj(kimlik,icerik):
icerikpak=kimlik+"@"+strftime("%Y-%m-%d %H:%M:%S", gmtime())+"\n"+icerik
dosya_adi=hashlib.md5(icerikpak.encode("utf")).hexdigest()
open(MESAJ_DIZINI+dosya_adi,"w").write(icerikpak)
return [dosya_adi,icerikpak]
class Vt():
connection = connect(VT_ADI)
def tablo_kontrol(self,tablo):
sql="SELECT name FROM sqlite_master WHERE type='table' AND name='"+tablo+"';"
sonuc=self.connection.execute(sql)
for row in sonuc:
return row
def tablo_olustur(self,ad):
if ad=="istemciler":
self.connection.execute("CREATE TABLE istemciler (id text primary key,sonaktif text,durum int)")
sonuc="tm"
else:
sonuc="olumsuz"
return sonuc
def tablo_veri_al(self,ad):
veri=[]
sonuc=self.connection.execute('SELECT * FROM '+ad)
for row in sonuc:
return row
def istemci_kontrol(self,kimlik):
sql="SELECT * FROM istemciler where id='"+kimlik+"'"
sonuc=self.connection.execute(sql)
if sonuc:
for row in sonuc:
return row
return None
def istemciler(self):
sonuc=self.connection.execute('SELECT * FROM istemciler')
for row in sonuc:
return row
def tablo_veri_ekle(self,ad,veri):
if ad=="istemciler":
self.connection.execute("INSERT INTO "+ad+" (id,sonaktif,durum) VALUES (?,?,?)",veri)
self.connection.commit()
return "tm"
def istemci_ekle(self,veri,mod):
tad="istemciler"
sql=""
if mod=="y":
sql="INSERT INTO "+tad+" (id,sonaktif,durum) VALUES (?,?,?)"
yeni_mesaj("sunucu",veri[0]+" kullanıcısı eklendi.")
else:
sql="UPDATE "+tad+" SET id = ? , sonaktif = ? , durum = ? WHERE id = ? "
veri.append(veri[0])
#print("istemci > ",veri)
self.connection.execute(sql,veri)
self.connection.commit()
return "tm"
class MilisBildirimSunucu(RPCServer):
cliste=[]
global vt
vt=Vt()
def calistir(self):
return "yapimda"
def tablo_olustur(self,sifre,vt_adi):
if sifre==SIFRE:
vt.tablo_olustur(ad)
def sum(self, x, y):
return x + y
def nabiz_at(self,kimlik):
kimlik=str(kimlik)
#kimlik kaydet
zaman=strftime("%Y-%m-%d %H:%M:%S", gmtime())
veri=[kimlik,zaman,1]
mod=""
if vt.istemci_kontrol(kimlik):
mod="g"
else:
mod="y"
snc=vt.istemci_ekle(veri,mod)
print (kimlik,mod)
#aktivlerin liste gönder
if snc=="tm":
return "sunucu bağlandı"
else:
return "hata"
def mesaj_al(self,kimlik,mliste):
#print (kimlik,">>>",mliste)
ymliste=gliste_al(mliste)
return ymliste
def mesaj_gonder(self,kimlik,icerik,sifre):
if sifre==SIFRE:
print (kimlik,">>> mesaj gönderdi.")
snc=yeni_mesaj(kimlik,icerik)
return snc
else:
return "yanlış şifre!"
def sor(self,param1):
if param1=="zaman":
return strftime("%Y-%m-%d %H:%M:%S", gmtime())
vtilk=Vt()
if vtilk.tablo_kontrol("istemciler") is None:
print("veritabanı ilklemesi yapılacak.")
snc=vtilk.tablo_olustur("istemciler")
if snc=="olumsuz":
print("Veritabanı oluşturmada sorun oluştu!")
sys.exit(0)
else:
print("veritabanı ilklemesi yapıldı.")
server = StreamServer(('0.0.0.0', 8003), MilisBildirimSunucu())
server.serve_forever()