LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2017, 09:37 PM   #1
luofeiyu
Member
 
Registered: Aug 2015
Posts: 211

Rep: Reputation: Disabled
How to get the webpage with socks5 proxy in python3?


sudo lsof -i:1080
Code:
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    sslocal 1795 root    4u  IPv4  16233      0t0  TCP localhost:socks (LISTEN)
    sslocal 1795 root    5u  IPv4  16234      0t0  UDP localhost:socks
An app was listening on localhost:1080,it is ready for curl's socks5 proxy.
The app provided socks5 proxy service is shadowsocks client on my pc.

curl can work with socks proxy in my pc.
[code]
target="target_url_youtube"
curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample
[/cdoe]
The target url can be downloaded with scoks5 proxy in curl.
Code:
    shadowsocks client------->shadowsocks server--->target_url_youtube
    127.0.0.1:1080          1xx.1xx.1xx.1xx:port       target_url_youtube
Notice:
All the packages from 127.0.0.1:1080 to 1xx.1xx.1xx.1xxort is sent and received by shadowsocks client and server.
curl just sent packages to 127.0.0.1:1080.


Now i want to get the target webpage with socks proxy in python3.

the first try :
Code:
    import urllib.request
    target="target_url_youtubr"
    proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)
    web = urllib.request.urlopen(target).read()
    print(web)

    The error info:
        sock.connect(sa)
    OSError: [Errno 101] Network is unreachable
Notice:
It is no use to write {'sock5': 'localhost:1080'} as {'sock5': '127.0.0.1:1080'},i have verified it.

the second try:
Code:
    import socks
    import socket
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)
    socket.socket = socks.socksocket
    import urllib.request
    target="target_url_youtubr"
    print(urllib.request.urlopen('target').read())
   
    error info:
    raise BadStatusLine(line)
    http.client.BadStatusLine:
The third try:
Code:
    import socks
    import socket
    from urllib import request
    socks.set_default_proxy(socks.SOCKS5, "localhost", 1080)
    socket.socket = socks.socksocket
    target="target_url_youtube"
    r = request.urlopen(url)
    print(r.read())

    ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:600)
    urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:600)>
Why data packet can't send via localhost:1080 and get the target_url_youtube's content,but curl can?
How to fix my python3 code for socks5 proxy?
Code:
    shadowsocks client---------->shadowsocks server------>target_url_youtube
    127.0.0.1:1080               1xx.1xx.1xx.1xx:port     target_url_youtube
    `curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample`  can  do the job.
why all the three python codes can't do?
How to fix it?

Last edited by luofeiyu; 09-22-2017 at 09:38 PM.
 
Old 09-23-2017, 01:07 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,317

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
Exactly what is the advantage of running a proxy on your own computer? Proxies are supposed to hide/protect the actual client, forwarding its trafic as if they were the sender.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I've set up a hotspot in linux,how to forward the network traffic to a socks5 proxy? MarkZar Linux - Networking 1 12-26-2014 07:15 AM
LXer: Redirect all (TCP) traffic through transparent socks5 proxy in Linux LXer Syndicated Linux News 0 01-28-2012 07:10 PM
socks5 proxy with bash? Dr_Death_UAE Linux - General 1 09-20-2009 11:00 AM
Programm that works simultaneously as a socks5 and http proxy axelfc Linux - Software 0 01-25-2006 01:26 PM
I want to download ftp-site files via wget and socks5 proxy server. jiawj Red Hat 2 10-28-2004 04:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration