Skip to content

Commit 3914d12

Browse files
committedJan 15, 2023
added comments
1 parent 7a52250 commit 3914d12

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed
 

‎client.py

-22
This file was deleted.

‎server.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1+
# This script creates a gRPC server that serves train station data by reading data from a sqlite3 database
12
import grpc
23
from concurrent import futures
34
import sqlite3
45

56
import stations_pb2 as stations_pb2
67
import stations_pb2_grpc as stations_pb2_grpc
78

8-
class StationsServicer(stations_pb2_grpc.StationsServicer):
9-
9+
# Connect to the SQLite3 database
10+
def connectDB():
11+
connection = sqlite3.connect('data/DataAnalyzer.db')
12+
return connection
1013

14+
# StationsServicer class is implemented to handle the gRPC requests
15+
class StationsServicer(stations_pb2_grpc.StationsServicer):
16+
# Read function is implemented to read the data from the sqlite3 database based on the zipcode passed in the request
1117
def Read(self, readRequestPB, context):
12-
18+
connectDB()
19+
1320
cp = readRequestPB.zipcode
14-
connection = sqlite3.connect('data/DataAnalyzer.db')
1521
cur = connection.cursor()
22+
23+
# sql query to fetch the data based on the zipcode
1624
sql = "SELECT gare_alias_libelle, gare_regionsncf, adresse_cp, departement, uic_code FROM referentiel WHERE adresse_cp = " + cp
17-
1825
exe = cur.execute(sql)
19-
2026
rslt = exe.fetchall()
21-
print(rslt)
22-
27+
28+
# list to hold the data to be returned as the response
2329
result = []
2430

2531
for i in rslt:
2632
response = stations_pb2.readResponsePB().Value()
2733
response.uic_code = str(i[4])
2834
response.gare_alias_libelle = str(i[0])
29-
response.gare_alias_libelle = str(i[3])
3035
response.adresse_cp = str(i[2])
3136
response.gare_regionsncf = str(i[1])
3237
result.append(response)
@@ -36,7 +41,8 @@ def Read(self, readRequestPB, context):
3641
print(response_list)
3742

3843
return response_list
39-
44+
45+
# serve function starts the gRPC server and listens on port 9600
4046
def serve():
4147

4248
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

‎stations.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ message readResponsePB {
1515
string uic_code = 2;
1616
string adresse_cp = 5;
1717
string gare_regionsncf = 23;
18-
string ui_code = 6;
18+
string l = 6;
1919

2020
}
2121
repeated Value value = 1;

0 commit comments

Comments
 (0)
Please sign in to comment.