-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadServer.py
More file actions
74 lines (54 loc) · 1.6 KB
/
threadServer.py
File metadata and controls
74 lines (54 loc) · 1.6 KB
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
import socket
import _thread
import addressbook_pb2
import sys
import numpy as np
#import pandas as pd
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
def help():
print("Hi there!\n")
print("Usage:", sys.argv[0],"\n")
sys.exit(-1)
HOST = '127.0.0.1' # Endereco IP do Servidor
PORT = 16785 # Porta que o Servidor esta
address_book = addressbook_pb2.AddressBook()
#Ia criar um dataframe pra trabalhar quase que
#completamente com operacoes em disco tornando muito eficiente consultas e operacoes como update e delete
#data = {'name':[],'id':[],'email':[],'phonetype':[],'phonenumber':[]}
#df = pd.DataFrame(data=d)
def readALL():
with open('adresbok', "rb") as f:
address_book.ParseFromString(f.read())
def delete(id):
with open('adresbok', "rb") as f:
address_book.ParseFromString(f.read())
def insert(msgs):
with open('adresbok', "ab") as f:
f.write(msgs)
def conectado(con, cliente):
print ('Conectado por', cliente)
while True:
msg = con.recv(1024)
if msg == b'1':
proto = con.recv(1024)
insert(proto)
if not msg: break
print (cliente, msg)
print ('Finalizando conexao do cliente', cliente)
con.close()
_thread.exit()
def main():
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
tcp.bind(orig)
tcp.listen(1)
while True:
con, cliente = tcp.accept()
_thread.start_new_thread(conectado, tuple([con, cliente]))
tcp.close()
if len(sys.argv) != 1:
help()
main()