-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.py
87 lines (72 loc) · 2.41 KB
/
link.py
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
86
87
# -*- coding: utf-8 -*-
"""
Created on Sun May 6 23:38:37 2018
@author: srg
"""
#!/usr/bin/python
#-*- coding: utf-8 -*-
import socket
#from sendfile import sendfile
import os.path
class link(object):
def __init__(self):
#request.__init__(self)
self.connected_by = []
# def connect(ip1_ser, ip2_cli,path):
# link.create_server(ip1_ser,path)
# link.create_client(ip1_ser)
def create_server(self,ip,path ): #ip is of the server
connection_list = []
print(ip)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((ip, 12345))
print("server created")
connection_list.append(sock)
sock.listen(1)
#offset = 0
file = open(path, "rb")
print("file opened")
print("waiting for connection...")
conn, addr = sock.accept()
print ('New connection from %s:%d' % (addr[0], addr[1]))
print("accepted connection")
connection_list.append(conn)
while True:
conn.send(str("Start").encode()) # THIS CAUSES BROKEN PIPE ERROR
chunk = file.read(65536)
print("chunk read")
if not chunk:
break # EOF
file.close()
conn.close()
#conn.send(str(chunk).encode())
conn.send(chunk)
print("chunk sent")
#conn.send(str("ENDED").encode())
print("Transfer complete")
conn.close()
def create_client(self,ip,file ): #ip of server
print(ip)
print("going to download",str(file))
try:
client=socket.create_connection((ip, 12345 ))
except:
client=socket.create_connection((ip, 12346 ))
print("client created")
dat=client.recv(4096)
with open(str(file), 'wb') as f:
socket_list = [client]
print("file opened")
while True:
print("started")
data=client.recv(65536)
dat=data
if not data:
break
else:
f.write(data)
print("recieved data")
print("Transfer complete")
f.close()
#time.sleep(5)
client.close()