-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
86 lines (73 loc) · 2.93 KB
/
server.py
File metadata and controls
86 lines (73 loc) · 2.93 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
75
76
77
78
79
80
81
82
83
84
85
# first of all import the socket library
import socket
import json, time
import os, sys
import datetime
from pymongo import MongoClient
from pprint import pprint
# serverStatusResult = db.command("serverStatus")
socket = socket.socket()
port = int(sys.argv[1])
socket.bind(('', port))
print("Socket is created with port %s" % port)
socket.listen(5)
print("Listening")
i = 0
while True:
connection, address = socket.accept()
print "Got Connected with ", address
i = i+1
start_time = time.time()
child_pid = os.fork()
if child_pid == 0:
print "Process",address
print i
socket.close()
while True:
data = connection.recv(2049)
if data == "read":
lines = open('wordfile.txt').read()
data1 = json.dumps(lines)
connection.send(data1)
if data == "write":
lines = open('wordfile.txt', 'a')
change = connection.recv(4098)
client = MongoClient("mongodb://priyagup:Codepri%40143@advancedropboxproject-shard-00-00-jdfx8.mongodb.net:27017,advancedropboxproject-shard-00-01-jdfx8.mongodb.net:27017,advancedropboxproject-shard-00-02-jdfx8.mongodb.net:27017/test?ssl=true&replicaSet=AdvanceDropboxproject-shard-0&authSource=admin&retryWrites=true")
# db = client.admin
db = client["AdvanceDropboxproject"]
col = db["History"]
i = address
now = datetime.datetime.now()
lis = {"port": i, "Time": str(now)}
x = col.insert_one(lis)
# print(x)
# print(yo)
lines.write(change)
if data == "update":
client = MongoClient("mongodb://priyagup:Codepri%40143@advancedropboxproject-shard-00-00-jdfx8.mongodb.net:27017,advancedropboxproject-shard-00-01-jdfx8.mongodb.net:27017,advancedropboxproject-shard-00-02-jdfx8.mongodb.net:27017/test?ssl=true&replicaSet=AdvanceDropboxproject-shard-0&authSource=admin&retryWrites=true")
# db = client.admin
db = client["AdvanceDropboxproject"]
col = db["History"]
for x in col.find():
print(x)
list_time = x.values()
list_time.pop(0)
arr = json.dumps(list_time)
connection.send(arr.encode())
lines.close()
if data == "4":
break
#
# now = datetime.datetime.now()
# lis = {"port": i, "Time": str(now)}
# x = col.insert_one(lis)
# array_of_word = []
# for words in data.split(","):
# array_of_word.append(words)
# result = mergeSort(array_of_word)
# # if not data:
# # break
# print i
os._exit(0)
print "child pid is %d" % child_pid
c.close()