1 | # -*- coding: utf-8 -*-<F12> |
---|
2 | |
---|
3 | import os.path |
---|
4 | import base64 |
---|
5 | import os |
---|
6 | import pwd |
---|
7 | import multiprocessing |
---|
8 | import socket |
---|
9 | import ssl |
---|
10 | import shutil |
---|
11 | |
---|
12 | from xmlrpclib import * |
---|
13 | |
---|
14 | class TeacherShare: |
---|
15 | |
---|
16 | def send_to_teacher_net(self,from_user,to_user,file_path): |
---|
17 | |
---|
18 | file_path=file_path.encode("utf8") |
---|
19 | #Get ip from user |
---|
20 | s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
---|
21 | s.connect(("server",9779)) |
---|
22 | student_ip=s.getsockname()[0] |
---|
23 | |
---|
24 | server = ServerProxy ("https://server:9779") |
---|
25 | |
---|
26 | try: |
---|
27 | paths=server.get_paths("","TeacherShareManager") |
---|
28 | |
---|
29 | except Exception as e: |
---|
30 | print e |
---|
31 | return False |
---|
32 | |
---|
33 | if to_user in paths: |
---|
34 | path,name,ip,port=paths[to_user] |
---|
35 | path=path.encode("utf8") |
---|
36 | name=name.encode("utf8") |
---|
37 | try: |
---|
38 | src=file_path |
---|
39 | queue=multiprocessing.Queue() |
---|
40 | p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,student_ip,ip,from_user,to_user,False,queue)) |
---|
41 | p.start() |
---|
42 | p.join() |
---|
43 | |
---|
44 | if not queue.get(): |
---|
45 | return False |
---|
46 | else: |
---|
47 | return True |
---|
48 | |
---|
49 | except Exception as e: |
---|
50 | print e |
---|
51 | return False |
---|
52 | #def send_to_teacher_net |
---|
53 | |
---|
54 | def copy_file_as_user(self,src,from_ip,to_ip,from_user,to_user,delete,queue): |
---|
55 | try: |
---|
56 | server=ServerProxy("https://"+to_ip+":9779") |
---|
57 | ret=server.grab_file("","TeacherShare",from_user,from_ip,src) |
---|
58 | if ret: |
---|
59 | if delete: |
---|
60 | os.remove(src) |
---|
61 | queue.put(True) |
---|
62 | else: |
---|
63 | queue.put(False) |
---|
64 | except Exception as e: |
---|
65 | print e |
---|
66 | queue.put(False) |
---|
67 | #def copy_file_as_user |
---|
68 | |
---|
69 | |
---|
70 | def grab_file(self,from_user,from_ip,src): |
---|
71 | if self.credentials: |
---|
72 | teacher_uid=pwd.getpwnam(self.credentials[0])[2] |
---|
73 | teacher_gid=pwd.getpwnam(self.credentials[0])[3] |
---|
74 | server=ServerProxy("https://localhost:9779") |
---|
75 | if self.credentials: |
---|
76 | try: |
---|
77 | fileName=os.path.basename(src) |
---|
78 | dest=self.shared_path+"/["+from_user+"]_"+fileName |
---|
79 | ret=server.get_file(self.credentials,"ScpManager",from_user,self.credentials[0],self.credentials[1],from_ip,src,dest) |
---|
80 | os.chown(dest,teacher_uid,teacher_gid) |
---|
81 | return True |
---|
82 | except Exception as e: |
---|
83 | print e |
---|
84 | return False |
---|
85 | else: |
---|
86 | print("No credentials found") |
---|
87 | return False |
---|
88 | |
---|
89 | def register_share_info(self,user,pwd,path): |
---|
90 | self.credentials=(user,pwd) |
---|
91 | self.shared_path=path |
---|
92 | #def register_credentials |
---|
93 | |
---|
94 | #class TeacherFileManager |
---|
95 | |
---|