-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.py
129 lines (118 loc) · 3.62 KB
/
lib.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Contains all the functions excluding the main image processing part (which is in serin.py)
import socket
import sys
from os.path import basename
import Tkinter, tkFileDialog
def printDone():
print '-------------------------------'
print '|||||| |||| |\\ || ||||||'
print '|| || || || ||\\ || || '
print '|| || || || || \\ || ||> '
print '|| || || || || \\|| || '
print '|||||| |||| || \\| ||||||'
print '-------------------------------'
def printSerin():
print '--------------------------------------------------'
print ' ________ ________ ______ _ __ _ '
print '| ______| | ______| | __ | | | | \ | |'
print '| |______ | |___ | |__| | | | | \ | |'
print '|______ | | ___| | __ | | | | |\ \ | |'
print ' ______| | | |______ | | \ \ | | | | \ \| |'
print '|________| |________| |_| \_\ |_| |_| \___|'
print '--------------------------------------------------'
# To create a connection to a socket server
def createConnTo(host, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
print 'Failed to create Socket'
sys.exit()
s.connect((host, port))
return s
# To send a file to a socket server
def sendFile(sock, fil):
try:
data = fil.read(1024)
while data != '':
sock.sendall(data)
data = fil.read(1024)
finally:
fil.close()
# To send a message to a socket server
def sendMsg(sock, msg):
try:
sock.sendall(msg)
except socket.error:
print 'Message Sending Failed'
sys.exit()
print 'Message Sent'
# To create a socket server here
def createListenerHere(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error, msg:
print 'Bind Failed. Errr Code : ' + str(msg[0]) + ' Message : ' + str(msg[1])
sys.exit()
return s
# The main action function. Called when event happens.
def act(serverip, imsender = 0):
conSock = createConnTo(serverip, 4444)
print 'Connected to Server'
# Asking for server status
sendMsg(conSock, 'status_of_server')
print 'Asking status of server'
reply = conSock.recv(1024)
conSock.close()
if reply == 'send_file':
print 'Ok, now I am sender'
# Choosing file
root = Tkinter.Tk()
root.withdraw()
toSend = tkFileDialog.askopenfilename()
toSendName = basename(toSend)
print 'You have selected ' + toSendName
conSock = createConnTo(serverip, 4444)
# Sending Filename to server
sendMsg(conSock, toSendName)
reply = conSock.recv(1024)
if reply == 'ok':
print 'Sent filename to server'
imsender = 1
conSock.close()
if reply:
# I m sender and i m broadcasting the file for reception
if imsender == 1:
filesender = createListenerHere('', 5555)
filesender.listen(1)
print 'Listening for file request'
while True:
conn, adr = filesender.accept()
print 'Connected to ' + adr[0] + ':' + str(adr[1])
req = conn.recv(1024)
if req == 'gimme':
print 'Connection asked for file, sending now.'
toSendFile = open(toSend, 'rb')
sendFile(conn, toSendFile)
break
printDone()
# I m receiver and m asking sender for file
else:
#file name and sender ip has been recieved
print 'Ready to recieve file'
replies = reply.split(',')
fileName = replies[0]
sender = replies[1]
sockToFS = createConnTo(sender, 5555)
fil = open(fileName, 'wb')
print 'Requesting sender for file'
sendMsg(sockToFS, 'gimme')
response = sockToFS.recv(1024)
print ' Recieving file '
while response != '':
fil.write(response)
response = sockToFS.recv(1024)
fil.close()
print 'File ' + fileName + ' received.'
sockToFS.close()
printDone()