Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the webpage with socks5 proxy in python3?

250 views
Skip to first unread message

Length Power

unread,
Sep 22, 2017, 9:44:37 PM9/22/17
to
sudo lsof -i:1080
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.

target="target_url_youtube"
curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample

The target url can be downloaded with scoks5 proxy in curl.

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.1xx:port 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 :

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:

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:as Martijn Pieters say in web

[Python3 - Requests with Sock5 proxy ][1]

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?

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?



[1]: https://stackoverflow.com/questions/31777692/python3-requests-with-sock5-proxy

Length Power

unread,
Sep 23, 2017, 12:00:08 PM9/23/17
to
0 new messages