Skip to content

Commit

Permalink
Feature proxy object (#31)
Browse files Browse the repository at this point in the history
* Added Vietnamese version of README.md file (#27)

* Created Proxy Object with ip, port, country and anonymity level for starters (#30)
  • Loading branch information
AngelouDi authored and pgaref committed Jun 26, 2017
1 parent 4a6032d commit 6d8d91b
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions README-vi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# HTTP Request Randomizer [![Build Status](https://travis-ci.org/pgaref/HTTP_Request_Randomizer.svg?branch=master)](https://travis-ci.org/pgaref/HTTP_Request_Randomizer) [![Coverage Status](https://coveralls.io/repos/github/pgaref/HTTP_Request_Randomizer/badge.svg?branch=master)](https://coveralls.io/github/pgaref/HTTP_Request_Randomizer?branch=master) [![Dependency Status](https://gemnasium.com/badges/github.com/pgaref/HTTP_Request_Randomizer.svg)](https://gemnasium.com/github.com/pgaref/HTTP_Request_Randomizer) [![PyPI version](https://badge.fury.io/py/http-request-randomizer.svg)](https://badge.fury.io/py/http-request-randomizer)

[View English version of this file here](README.md)

Thư viện này cung cấp để cải tiến cách thực thi các request HTTP của thư viện **requests** trong Python. Một trong những tính năng rất cần thiết đó là hỗ trợ proxy. HTTP được thiết kế rất tốt để làm việc với proxy.

Proxy rất hữu ích khi làm các công việc liên quan đến thu thập dữ liệu web hoặc đơn giản là khi bạn muốn ẩn danh (anomization).

Trong dự án này tôi sử dụng các proxy được cung cấp trên mạng và sử dụng nhiều user-agent khác nhau để gửi các request http bằng những IP ngẫu nhiên.

## Proxy là gì

Proxy cho phép sử dụng máy chủ P (trung gian) để liên lạc với máy chủ A và sau đó phản hồi lại cho bạn kết quả. Một cách nói khác, việc này giúp ẩn đi sự hiện diện của bạn khi truy cập vào một trang web, website sẽ hiểu đây là truy cập từ nhiều người thay vì chỉ một người duy nhất.

Thông thường, các trang web sẽ chặn các địa chỉ IP gửi quá nhiều request, và proxy là một cách để giải quyết vấn đề này. Bạn có thể lợi dụng proxy để thực hiện tấn công một website, nhưng tốt hơn bạn nên hiểu cách proxy hoạt động như thế nào ;)

## User-Agent là gì

User-agent chỉ là một giá trị gửi kèm trong HTTP request để giúp máy chủ web có thể giả lập trình duyệt và gửi yêu cầu đến một website bất kỳ.

## Mã nguồn

Mã nguồn trong repository này sẽ thực hiện lấy proxy từ **bốn** website khác nhau:
* http://proxyfor.eu/geo.php
* http://free-proxy-list.net
* http://rebro.weebly.com/proxy-list.html
* http://www.samair.ru/proxy/time-01.htm

Sau khi thu thập danh sách các proxy và loại bỏ những proxy chậm nó sẽ lấy ngẫu nhiên một proxy để gửi request đến url được chỉ định.
Thời gian chờ được thiết lập là 30 giây và nếu proxy không phản hồi kết quả nó sẽ được xóa bỏ trong danh sách proxy.
Tôi phải nhắc lại rằng mỗi request được gửi bằng một user-agent khác nhau, danh sách user-agent (khoảng 900 chuỗi khác nhau) được lưu trong file **/data/user_agents.txt**

## Làm sao để sử dụng?

Project này đã được phân phối như là một thư viện PyPI!
Đây là phần source code mẫu cho việc sử dụng thư viện này. Bạn chỉ cần thêm **http-request-randomizer** vào file requirements.txt và chạy đoạn code dưới đây:

````python
import time
from http_request_randomizer.requests.proxy.requestProxy import RequestProxy

if __name__ == '__main__':

start = time.time()
req_proxy = RequestProxy()
print("Initialization took: {0} sec".format((time.time() - start)))
print("Size: {0}".format(len(req_proxy.get_proxy_list())))
print("ALL = {0} ".format(req_proxy.get_proxy_list()))

test_url = 'http://ipv4.icanhazip.com'

while True:
start = time.time()
request = req_proxy.generate_proxied_request(test_url)
print("Proxied Request Took: {0} sec => Status: {1}".format((time.time() - start), request.__str__()))
if request is not None:
print("\t Response: ip={0}".format(u''.join(request.text).encode('utf-8')))
print("Proxy List Size: {0}".format(len(req_proxy.get_proxy_list())))

print("-> Going to sleep..")
time.sleep(10)
````

## Tài liệu

[http-request-randomizer documentation](http://pythonhosted.org/http-request-randomizer)


## Đóng góp

Mọi đóng góp của bạn luôn được trân trọng. Đừng ngần ngại gửi pull request cho chúng tôi.

## Bạn gặp vấn đề với thư viện này?

Hãy nêu lên vấn đề của bạn [tại đây](https://github.com/pgaref/HTTP_Request_Randomizer/issues), và càng chi tiết càng tốt. Chúng tôi sẽ hỗ trợ bạn trong sớm nhất có thể :)

## Giấy phép

Dự án này được cấp phép theo các điều khoản của giấy phép MIT.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HTTP Request Randomizer [![Build Status](https://travis-ci.org/pgaref/HTTP_Request_Randomizer.svg?branch=master)](https://travis-ci.org/pgaref/HTTP_Request_Randomizer) [![Coverage Status](https://coveralls.io/repos/github/pgaref/HTTP_Request_Randomizer/badge.svg?branch=master)](https://coveralls.io/github/pgaref/HTTP_Request_Randomizer?branch=master) [![Dependency Status](https://gemnasium.com/badges/github.com/pgaref/HTTP_Request_Randomizer.svg)](https://gemnasium.com/github.com/pgaref/HTTP_Request_Randomizer) [![PyPI version](https://badge.fury.io/py/http-request-randomizer.svg)](https://badge.fury.io/py/http-request-randomizer)

[View Vietnamese version of this file here](README-vi.md)

A convenient way to implement HTTP requests is using Pythons' **requests** library.
One of requests’ most popular features is simple proxying support.
HTTP as a protocol has very well-defined semantics for dealing with proxies, and this contributed to the widespread deployment of HTTP proxies
Expand Down
9 changes: 9 additions & 0 deletions http_request_randomizer/requests/parsers/FreeProxyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bs4 import BeautifulSoup

from http_request_randomizer.requests.parsers.UrlParser import UrlParser
from http_request_randomizer.requests.proxy.ProxyObject import ProxyObject

logger = logging.getLogger(__name__)
__author__ = 'pgaref'
Expand Down Expand Up @@ -45,12 +46,20 @@ def parse_proxyList(self):
break
else:
address += field[1] + ':'
proxy_object = ProxyObject()
proxy_object.ip_address = field[1]
elif field[0] == 'Port':
address += field[1]
proxy_object.port = field[1]
elif field[0] == 'Anonymity':
proxy_object.anonymity_level = field[1]
elif field[0] == 'Country':
proxy_object.country = field[1]
# Make sure it is a Valid Proxy Address
if UrlParser.valid_ip_port(address):
proxy = "http://" + address
curr_proxy_list.append(proxy.__str__())
proxy_object.print_everything()
else:
logger.debug("Address with Invalid format: {}".format(address))
# print "{0:<10}: {1}".format(field[0], field[1])
Expand Down
9 changes: 9 additions & 0 deletions http_request_randomizer/requests/parsers/ProxyForEuParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bs4 import BeautifulSoup

from http_request_randomizer.requests.parsers.UrlParser import UrlParser
from http_request_randomizer.requests.proxy.ProxyObject import ProxyObject

logger = logging.getLogger(__name__)
__author__ = 'pgaref'
Expand Down Expand Up @@ -49,12 +50,20 @@ def parse_proxyList(self):
break
else:
address += field[1] + ':'
proxy_object = ProxyObject()
proxy_object.ip_address = field[1]
elif field[0] == 'Port':
address += field[1]
proxy_object.port = field[1]
elif field[0] == 'Anon':
proxy_object.anonymity_level = field[1]
elif field[0] == 'Country':
proxy_object.country = field[1]
# Avoid Straggler proxies and make sure it is a Valid Proxy Address
if not proxy_straggler and UrlParser.valid_ip_port(address):
proxy = "http://" + address
curr_proxy_list.append(proxy.__str__())
proxy_object.print_everything()
# print "{0:<10}: {1}".format(field[0], field[1])
# print "ALL: ", curr_proxy_list
return curr_proxy_list
Expand Down
13 changes: 13 additions & 0 deletions http_request_randomizer/requests/parsers/RebroWeeblyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bs4 import BeautifulSoup

from http_request_randomizer.requests.parsers.UrlParser import UrlParser
from http_request_randomizer.requests.proxy.ProxyObject import ProxyObject

logger = logging.getLogger(__name__)
__author__ = 'pgaref'
Expand Down Expand Up @@ -33,6 +34,12 @@ def parse_proxyList(self, use_top15k=False):
if UrlParser.valid_ip_port(row):
proxy = "http://" + row
curr_proxy_list.append(proxy.__str__())
proxy_object = ProxyObject()
proxy_object.ip_address = row.split(":")[0]
proxy_object.port = row.split(":")[1]
proxy_object.country = "Yet to be implemented"
proxy_object.anonymity_level = "Elite or Anonymous"
proxy_object.print_everything()
else:
logger.debug("Address with Invalid format: {}".format(row))
# Usually these proxies are stale
Expand All @@ -49,6 +56,12 @@ def parse_proxyList(self, use_top15k=False):
for proxy_address in more_content.split():
if UrlParser.valid_ip_port(proxy_address):
curr_proxy_list.append(proxy_address)
proxy_object = ProxyObject()
proxy_object.ip_address = proxy_address.split(":")[0]
proxy_object.port = proxy_address.split(":")[1]
proxy_object.country = "Unknown"
proxy_object.anonymity_level = "Elite or Anonymous"
proxy_object.print_everything()

return curr_proxy_list

Expand Down
10 changes: 10 additions & 0 deletions http_request_randomizer/requests/parsers/SamairProxyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bs4 import BeautifulSoup

from http_request_randomizer.requests.parsers.UrlParser import UrlParser
from http_request_randomizer.requests.proxy.ProxyObject import ProxyObject

logger = logging.getLogger(__name__)
__author__ = 'pgaref'
Expand Down Expand Up @@ -46,6 +47,15 @@ def parse_proxyList(self):
# Make sure it is a Valid Proxy Address
if UrlParser.valid_ip_port(td_row.text):
curr_proxy_list.append('http://' +td_row.text)
proxy_object = ProxyObject()
proxy_object.ip_address = td_row.text.split(":")[0]
proxy_object.port = td_row.text.split(":")[1]
next_td_row = td_row.findNext("td")
proxy_object.anonymity_level = next_td_row.text
next_td_row = next_td_row.findNext("td")
next_td_row = next_td_row.findNext("td")
proxy_object.country = next_td_row.text
proxy_object.print_everything()
else:
logger.debug("Address with Invalid format: {}".format(td_row.text))

Expand Down
11 changes: 11 additions & 0 deletions http_request_randomizer/requests/proxy/ProxyObject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProxyObject(object):

def __int__(self, ip_address, port, anonymity_level, country):
self.ip_address = ip_address
self.port = port
self.anonymity_level = anonymity_level
self.country = country

def print_everything(self):
print("Address: {0} | Port: {1} | Country: {2} | Anonymity: {3}" \
.format(self.ip_address, self.port, self.country, self.anonymity_level))

0 comments on commit 6d8d91b

Please sign in to comment.