Skip to content

Examples

YuDe edited this page Jan 2, 2018 · 2 revisions

Example code.

We hope the example code here can give people a shiny beacon of hope.

Basic Usage

Example 1: GET bbc.com

sneak can be used to develop crawlers just like other famous python package.

In [1]: from sneak.Http import Session

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server 
Jan 02 09:33:35.000 [notice] Bootstrapped 10%: Finishing handshake with directory server 
Jan 02 09:33:36.000 [notice] Bootstrapped 15%: Establishing an encrypted directory connection 
Jan 02 09:33:36.000 [notice] Bootstrapped 20%: Asking for networkstatus consensus 
Jan 02 09:33:37.000 [notice] Bootstrapped 25%: Loading networkstatus consensus 
Jan 02 09:33:39.000 [notice] Bootstrapped 45%: Asking for relay descriptors 
Jan 02 09:33:41.000 [notice] Bootstrapped 68%: Loading relay descriptors 
Jan 02 09:33:43.000 [notice] Bootstrapped 75%: Loading relay descriptors 
Jan 02 09:33:43.000 [notice] Bootstrapped 80%: Connecting to the Tor network 
Jan 02 09:33:44.000 [notice] Bootstrapped 85%: Finishing handshake with first hop 
Jan 02 09:33:45.000 [notice] Bootstrapped 90%: Establishing a Tor circuit 
Jan 02 09:33:46.000 [notice] Bootstrapped 100%: Done  

In [3]: r = s.get('http://www.bbc.com')
start decompressing: gzip  

In [4]: r.status
Out[4]: 200  

In [5]: r.body
Out[5]: ' <!DOCTYPE html> <html class=" b-pw-1280" lang="en" > <head> <!-- Barlesque 3.21.31 --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />....

Example 2: GET onion site http://msydqstlz2kzerdg.onion

Crawl dark service is also possible.

In [1]: from sneak.Http import Session

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...
In [3]: r = s.get_onion('http://msydqstlz2kzerdg.onion')
start decompressing: gzip

In [4]: r.status
Out[4]: 200

In [5]: r.body
Out[5]: '\n\n\n<!doctype html>\n<html class="no-js" lang="en">\n  <head>\n      <meta charset="utf-8">\n ....

Example 3: HEAD a regular site.

You can use sneak to HEAD.

In [1]: from sneak.Http import Session

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...
In [3]: r = s.head('https://twitter.com/?lang=en')

In [4]: r.status
Out[4]: 200

In [5]: r.body
Out[5]: ''

Example 4: HEAD an onion site.

Onion site can be HEAD as well.

In [1]: from sneak.Http import Session

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...

In [3]: r = s.head_onion('http://greenroxwc5po3ab.onion/')

In [4]: r.status
Out[4]: 200

In [5]: r.body
Out[5]: ''

Example 5: POST

sneak can do POST handy.

In [1]: from sneak.Http import Session
In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...
In [3]: r = s.post('https://httpbin.org/post', data={1:1, 2:2})
start decompressing: gzip
Error -3 while decompressing data: incorrect header check

In [4]: r.status
Out[4]: 200

In [5]: r.body
Out[5]: '{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "1": "1", \n    "2": "2"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/'...

Example 6: POST on an onion site http://pms5n4czsmblkcjl.onion/

We use sneak to pretend we want to order something bad.
[WARNING] DO NOT order anything from this site, they are all illegal.

In [1]: from sneak.Http import Session

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...

In [3]: r = s.post_onion(
   ...:  'http://pms5n4czsmblkcjl.onion/cart.php',            
   ...:   data={ 'id': 100,'add': 'action','text': 2})  

In [4]: r.status
Out[4]: 200

In [5]: r.body
Out[5]: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\r\n<html>\r\n<head>\r\n<meta http-equiv="Content-type" content="text/html; charset=utf-8">...

Example 7: Renew Identity.

By calling the function renew_identity, developers can change their exit node's IP address.

In [2]: s = Session()
Jan 02 09:33:34.000 [notice] Bootstrapped 0%: Starting 
Jan 02 09:33:35.000 [notice] Bootstrapped 5%: Connecting to directory server
...

In [3]: r = s.get('https://httpbin.org/ip')

In [4]: r.body
Out[4]: '{\n  "origin": "173.255.226.142"\n}\n'

In [5]: s.renew_identity()
[Update] Renewing the identity... 
[Finished] Identity has been renewed. 

In [6]: r = s.get('https://httpbin.org/ip')

In [7]: r.body
Out[7]: '{\n  "origin": "65.19.167.130"\n}\n'