-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
180 lines (165 loc) · 5.23 KB
/
Copy pathserver.py
File metadata and controls
180 lines (165 loc) · 5.23 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import socket
import os
import sys
from Tkinter import *
import tkMessageBox
def getip(): #Function to retrieve server's Local IP using an active innternet connection.
p = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
p.connect(("gmail.com",80)) #Pinging any random site--->Here gmail.com
ip=(p.getsockname()[0])
return ip
p.close()
raise Exception() #Handling the exception if there is no active internet connection.
global port
port=0000
try:
script,port=sys.argv #Taking port as Command Line Argument and Handling the error in case the user doesn't enter the port
except:
root = Tk() #Pops up an error message
root.wm_title("Error")
label_error = Label(root, text = "Incorrect parameters\nUsage: python server.py PORT\nEx. python server.py 8000")
b = Button(root, text = "Okay", command = root.destroy)
label_error.pack()
b.pack()
root.mainloop()
sys.exit(1)
s = socket.socket() #Creating a Socket Object which will be used to recieve information from the server
try:
global ip
ip=getip()
except Exception:
#Will take ip address manually as the server is not connected to internet.
'''
GUI for taking input
'''
app = Tk()
app.wm_title("PySend")
global entry_ip
ip = 0
label_ip=Label(app, text = 'You are not connected to internet so we are not able to fetch your ip address\nPlease Enter ip manually:')
entry_ip=Entry(app, bd = 10)
def get_ip():
global ip
ip = entry_ip.get()
app.destroy()
b_ip = Button(app, text = "Create Server", command = get_ip)
label_ip.pack()
entry_ip.pack()
b_ip.pack()
app.mainloop()
try:
s.bind((ip,int(port))) #Hosting server using inbuilt bind function and handling the exception of already used socket as follows.
except:
sock=Tk()
sock.wm_title("Socket Error")
def exit1():
print 'Exited abruptly!'
sys.exit(1)
sock.protocol('WM_DELETE_WINDOW',exit1)
label_socket=Label(sock,text='Socket already in use\nPlease Enter another port to create server:')
entry_port=Entry(sock,bd=10)
port=8000 #Default is 8000
def createport():
global port
port=entry_port.get()
s.bind((ip,int(port)))
sock.destroy()
b_socket=Button(sock,text='Create Server!',command=createport) #Taking new Port to host server on.
label_socket.pack()
entry_port.pack()
b_socket.pack()
sock.mainloop()
root1 = Tk()
root1.wm_title("Server")
def exit1():
print 'Exited abruptly!'
sys.exit(1)
root1.protocol('WM_DELETE_WINDOW',exit1)
label_error = Label(root1, text = 'Server created successfully\nConnect to ip:'+ip+' and Port:'+str(port)+'\nWaiting for Connection!!')
b = Button(root1, text = "Okay", command = root1.destroy)
label_error.pack()
b.pack()
root1.mainloop()
s.listen(5) #This Functions waits for connection for 5 tries from client.
c, addr = s.accept()
root2 = Tk()
root2.wm_title("Connected")
def exit1():
print 'Exited abruptly!'
sys.exit(1)
root2.protocol('WM_DELETE_WINDOW',exit1)
label_error = Label(root2, text = 'Got connection from' + str(addr))
def destroyb():
root2.destroy()
b = Button(root2, text = "Okay", command = destroyb) #Destroys the Window
label_error.pack()
b.pack()
root2.mainloop()
c.send("Incoming File") #Send Function is used to send strings to the client.
app1=Tk()
def exit1():
print 'Exited abruptly!'
sys.exit(1)
app1.protocol('WM_DELETE_WINDOW',exit1)
app1.wm_title("Name")
global entry_name
name='abcd' #Default
#Asking User to enter the name of file located in the same folder as on which the code is saved.
label_name=Label(app1,text='Enter name of the file:')
entry_name=Entry(app1,bd=10)
def get_name(): #Selects and Stores the name.
global name, app1
name=entry_name.get()
app1.destroy()
b_name=Button(app1,text='Select this file!',command=get_name) #Calls the get_name function
label_name.pack()
entry_name.pack()
b_name.pack()
app1.mainloop()
print name
global byte
byte=0
def send_file():
global app2
app2.destroy()
c.send(name) #Sends the name of the file to the Client
i=0
def computeSize(): #Computes the size of file in bytes using the OS module
size=os.stat(name)
return size.st_size
try:
#global byte
byte=computeSize()
except:
print 'Not able to compute the size of file.Check if the file exits and try again.'
sys.exit(1)
#Converting size into 10 digit decimal number and converting into string and then sending the size to the client.
c.send(str(byte)+'.'+'0'*(9-len(str(byte))))
with open(name,'r') as fo: #Opening file as READ MODE and naming object as fo.
for j in range(byte+1):
line=fo.read(1) #Running loop and reading the file byte by byte and storing data in a variable.
try: #Handling Error of wrongly sent file size
c.send(line) #Now sending this variable to the client.
except:
print 'Some Error occurred.\nPlease try again.'
sys.exit(1)
fo.close()
app4=Tk()
def exit1():
print 'Exited abruptly!'
sys.exit(1)
app4.protocol('WM_DELETE_WINDOW',exit1)
def destroyb1():
app4.destroy()
b_sent=Button(app4,text='File('+name+') Sent Successfully!',command=destroyb1)
b_sent.pack()
app4.mainloop()
app2=Tk()
def exit1():
print 'Exited abruptly!'
sys.exit(1)
app2.protocol('WM_DELETE_WINDOW',exit1)
app2.wm_title("Send")
b_send=Button(app2,text='Send File!',command=send_file) #Will call send_file function
b_send.pack()
app2.mainloop()