-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
85 lines (78 loc) · 2.86 KB
/
Copy pathserver.py
File metadata and controls
85 lines (78 loc) · 2.86 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
#!/usr/bin/python
"""
Copyright (c) 2014 Nathan Haugo nhaugo@gmail.com
All rights reserved.
Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the <organization>. The name of the
<organization> may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"""
import argparse
import BeautifulSoup
import daemon
import logging
import tornado.ioloop
import tornado.options
import tornado.web
import urllib2
class MainHandler(tornado.web.RequestHandler):
def get(self, slug):
img = "http://www.submitawebsite.com/blog/wp-content/uploads/2010/06/404.png"
body = '<img src="http://www.submitawebsite.com/blog/wp-content/uploads/2010/06/404.png">'
if slug == 'i':
body = '<img src="%s">' % img
else:
url = "http://%s.jpg.to" % (slug)
response = urllib2.urlopen(url)
body = response.read()
try:
img = BeautifulSoup.BeautifulSoup(body).img['src']
except:
print "Bad img in %s" % body
data = """<html>
<head>
<meta content="%s" property="og:image"/>
</head>
<body>
%s
</body>
</html>
""" % (img, body)
self.write(data)
application = tornado.web.Application([ (r"/([^/]+)", MainHandler)])
def start_server(port, debug, log_file):
tornado.options.parse_command_line()
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
def setup():
parser = argparse.ArgumentParser()
parser.add_argument('--port', default=8080,
help='http server port')
parser.add_argument('--debug', action='store_true',
help='enable tornado debug mode')
parser.add_argument('--foreground', action='store_true',
help='run server in foreground')
parser.add_argument('--log-file', default="logs/tornado.log",
help='output log when daemonized')
args = parser.parse_args()
return args.port, args.debug, args.foreground, args.log_file
def main():
port, debug, fg, log_file = setup()
print log_file
log_fh = open(log_file, 'a+')
#logging.basicConfig(filename=log_file)
if fg:
return start_server(port, debug)
else:
daemon_context = daemon.DaemonContext(stdout=log_fh, stderr=log_fh)
with daemon_context:
return start_server(port, debug, log_file)
if __name__ == "__main__":
main()