-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnirzari.py
More file actions
96 lines (72 loc) · 2.96 KB
/
nirzari.py
File metadata and controls
96 lines (72 loc) · 2.96 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
from bottle import route, run, get, post, request, response
import socket
import sys
import requests
import threading
import json
import os
#Server_Location is the directory from where the program is being initialized.
#Server Socket is created. It listens on port number 8085.
try:
serv_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
host = 'localhost'
port = 8085
#a simple HTML page with a form which takes the file as an Input and sends it to the server.
@route('/uploadfile')
def uploadfile():
return '''
<form action="/uploadfile" method="post" enctype="multipart/form-data">
Select a file: <input type="file" name="myfile" />
<input type="submit" value="Upload" />
</form>
'''
#function to process the POST request made by the client which uploads the file at the Server_Location.
@route('/uploadfile', method='POST')
def start_upload():
uploadfile = request.files.get('myfile')
name, ext = os.path.splitext(uploadfile.filename)
storePath = os.getcwd()
if not os.path.exists(storePath):
os.makedirs(storePath)
file_path = "{path}/{myfile}".format(path=storePath, myfile=uploadfile.filename)
uploadfile.save(file_path)
fileOut = open("log.txt",'a')
response.status = 200
fileOut.write(str(response.status)+ "\n")
fileOut.write(str(os.stat(uploadfile.filename))+"\n")
return "File stored at '{0}'.".format(storePath)
#a simple HTML page with a form which asks for the filename as an Input for downloading.
#The file should be at the Server_Location otherwise it will not be found.
@get('/downloadfile')
def downloadfile():
return '''
<form action="/downloadfile" method="POST">
Select a file: <input type="text" name="mydownloadfile" />
<input type="submit" value="download" />
</form>
'''
#a function to download the file save it in the download directory specified by download_directory.
#change the download_directory for saving the file.
@post('/downloadfile')
def start_download():
downloadfile = request.forms.get('mydownloadfile')
fileToDownload = open(os.getcwd()+"/"+downloadfile,'r')
#Please change the path of download_directory according to the system
download_directory = "/home/ugupta/Downloads"
var1 = open(download_directory+"/"+downloadfile,'wb')
file_content = fileToDownload.read()
var1.write(file_content)
var2=os.path.basename(os.getcwd()+"/"+downloadfile)
with open("log.txt",'a') as fileOut:
responseCode = str(response.status)
if responseCode==200:
message = file_content
print("File Content is" % message)
if responseCode==404:
print("HTTP 404 file not found")
fileOut.write("GET - "+responseCode+"\n")
fileOut.write(str(os.stat(var2))+"\n")
threading.Thread(target=run, kwargs=dict(host='localhost', port=8085)).start()