-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchangeServer.py
More file actions
77 lines (66 loc) · 3.22 KB
/
changeServer.py
File metadata and controls
77 lines (66 loc) · 3.22 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
# first of all import the socket library
import socket
import json, pickle
import os, sys, datetime
from pymongo import MongoClient
# next create a socket object
class socketProgram(object):
def __init__(self):
#self.data = []
self.flag =0
def main(self):
s = socket.socket()
print "Socket successfully created"
port = int(sys.argv[1])
s.bind(('', port))
print "socket binded to %s" %(port)
# put the socket into listening mode
s.listen(5)
print "socket is listening"
while True:
# Establish connection with client.
connection, addr = s.accept()
print 'Got connection from', addr
print(self.flag)
pid=os.fork()
if (pid)==0:
print "New child created for client request"
s.close()
# send a thank you message to the client.
#Echo code
while True:
data = connection.recv(2048)
if data == "read":
lines = open('wordfile.txt').read()
data1 = json.dumps(lines)
connection.send(data1)
if data == "write":
self.flag =1
print(self.flag)
lines = open('wordfile.txt', 'a')
change = connection.recv(4096)
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["AdvanceDropboxproject"]
col = db["History"]
i = addr
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["AdvanceDropboxproject"]
col = db["History"]
for x in col.find():
print(x)
print(x.encode())
connection.send(x.encode())
lines.close()
if data == "4":
break
if __name__== "__main__":
socketProgram().main()
# Close the connection with the client
c.close()