में वेब प्रॉक्सी कैसे लिखें I python में एक वेब प्रॉक्सी लिखने की कोशिश कर रहा हूं। लक्ष्य यूआरएल जैसे http://proxyurl/http://anothersite.com/
पर जाना है और उसे सामान्य रूप से http://anothersite.com
की सामग्री देखें। मुझे अनुरोध पुस्तकालय का दुरुपयोग करके सौभाग्य से दूर मिल गया है, लेकिन यह वास्तव में अनुरोध ढांचे का इच्छित उपयोग नहीं है। मैंने पहले twisted के साथ प्रॉक्सी लिखी है, लेकिन मुझे यकीन नहीं है कि मैं इसे करने का प्रयास कर रहा हूं। यहां बताया गया है जहां मैं अब तक यहां हूं ...पायथन
import os
import urlparse
import requests
import tornado.ioloop
import tornado.web
from tornado import template
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
loader = template.Loader(path(ROOT, 'templates'))
class ProxyHandler(tornado.web.RequestHandler):
def get(self, slug):
if slug.startswith("http://") or slug.startswith("https://"):
if self.get_argument("start", None) == "true":
parsed = urlparse.urlparse(slug)
self.set_cookie("scheme", value=parsed.scheme)
self.set_cookie("netloc", value=parsed.netloc)
self.set_cookie("urlpath", value=parsed.path)
#external resource
else:
response = requests.get(slug)
headers = response.headers
if 'content-type' in headers:
self.set_header('Content-type', headers['content-type'])
if 'length' in headers:
self.set_header('length', headers['length'])
for block in response.iter_content(1024):
self.write(block)
self.finish()
return
else:
#absolute
if slug.startswith('/'):
slug = "{scheme}://{netloc}{original_slug}".format(
scheme=self.get_cookie('scheme'),
netloc=self.get_cookie('netloc'),
original_slug=slug,
)
#relative
else:
slug = "{scheme}://{netloc}{path}{original_slug}".format(
scheme=self.get_cookie('scheme'),
netloc=self.get_cookie('netloc'),
path=self.get_cookie('urlpath'),
original_slug=slug,
)
response = requests.get(slug)
#get the headers
headers = response.headers
#get doctype
doctype = None
if '<!doctype' in response.content.lower()[:9]:
doctype = response.content[:response.content.find('>')+1]
if 'content-type' in headers:
self.set_header('Content-type', headers['content-type'])
if 'length' in headers:
self.set_header('length', headers['length'])
self.write(response.content)
application = tornado.web.Application([
(r"/(.+)", ProxyHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
बस एक ध्यान दें, मैं अगर वहाँ क्वेरी स्ट्रिंग में सच शुरू = योजना, netloc, और urlpath संरक्षित करने के लिए एक कुकी सेट। इस तरह, किसी भी रिश्तेदार या पूर्ण लिंक जो प्रॉक्सी को हिट करता है उस कुकी को पूर्ण यूआरएल को हल करने के लिए उपयोग करता है।
इस कोड के साथ, यदि आप http://localhost:8888/http://espn.com/?start=true
पर जाते हैं तो आप ईएसपीएन की सामग्री देखेंगे। हालांकि, निम्नलिखित साइट पर यह बिल्कुल काम नहीं करता है: http://www.bottegaveneta.com/us/shop/। मेरा सवाल है, ऐसा करने का सबसे अच्छा तरीका क्या है? क्या वर्तमान तरीका मैं इस मजबूत को लागू कर रहा हूं या इस तरह से ऐसा करने के लिए कुछ भयानक नुकसान हैं? यदि यह सही है, तो कुछ ऐसी साइटें क्यों हैं जिन्हें मैंने बिल्कुल काम नहीं किया है?
किसी भी मदद के लिए धन्यवाद।
बोटेगा वेनेटा आपको सीधे संसाधनों तक पहुंचने नहीं देता है। उदाहरण के लिए, http://www.bottegaveneta.com/us/shop/css/bottegaveneta/form.css पर क्लिक करने का प्रयास करें - मुझे एक HTML 404 पृष्ठ मिलता है। –
मुझे लगता है कि यह HTTP रेफरर के साथ करना है। आप इसे भी सेट करने का प्रयास कर सकते हैं। –
@ कोले ओह, आपका मतलब रेफरर है? (https://en.wikipedia.org/wiki/HTTP_referer#Origin_of_the_term_referer) – rakslice