How to get the webpage with socks5 proxy in python3?

2017-09-23 Thread Length Power
sudo lsof -i:1080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sslocal 1795 root4u  IPv4  16233  0t0  TCP localhost:socks
(LISTEN)
sslocal 1795 root5u  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:

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: 

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?
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get the webpage with socks5 proxy in python3?

2017-09-23 Thread Length Power
sudo lsof -i:1080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sslocal 1795 root4u  IPv4  16233  0t0  TCP localhost:socks (LISTEN)
sslocal 1795 root5u  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: 

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
-- 
https://mail.python.org/mailman/listinfo/python-list


how to select all the monday form "20150101" till "20150620"?

2015-01-05 Thread length power
import pandas as pd
rng = pd.date_range("20150101","20150620",freq="D")
for day in rng:
   x = pd.to_datetime(day)
   y = x.timetuple().tm_wday
   if(y == 0) :print(x.strftime("%Y%m%d"))


I have selected all  the monday form "20150101" till "20150620",how to make
it more simple?
-- 
https://mail.python.org/mailman/listinfo/python-list


is there more simple way to do?

2014-04-09 Thread length power
x='name,sex,birthday\n\nx1,male,1948/05/28\n\nx2,female,1952/03/27
\n\nx3,female,1994/12/09'
x.replace("\n\n","\n").splitlines()

is there more simple way to replace  `x.replace("\n\n","\n").splitlines()` ?
-- 
https://mail.python.org/mailman/listinfo/python-list


how to insert the elements in a list properly?

2014-04-09 Thread length power
word=["x1","x2","x3","x4","x5"]
w=word[-2:]
del word[-2:]
word.insert(2,w)
word
['x1', 'x2', ['x4', 'x5'], 'x3']

what i want to get is
['x1', 'x2', 'x4', 'x5', 'x3']

how can i insert them ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to insert the elements in a list properly?

2014-04-09 Thread length power
words = ["x1", "x2", "x3", "x4", "x5"]
words.append(words.pop(2))
words.append(words.pop(2))
words
['x1', 'x2', 'x5', 'x3', 'x4']
why i can't write it as:

[words.append(words.pop(2)) for i in range(0,2)]

>>> [words.append(words.pop(2)) for i in range(0,2)]
[None, None]


2014-04-09 18:46 GMT+08:00 Peter Otten <__pete...@web.de>:

> length power wrote:
>
> > word=["x1","x2","x3","x4","x5"]
> > w=word[-2:]
> > del word[-2:]
> > word.insert(2,w)
> > word
> > ['x1', 'x2', ['x4', 'x5'], 'x3']
> >
> > what i want to get is
> > ['x1', 'x2', 'x4', 'x5', 'x3']
> >
> > how can i insert them ?
>
> Make the left-hand side an empty slice:
>
> >>> words = ["x1", "x2", "x3", "x4", "x5"]
> >>> w = words[-2:]
> >>> del words[-2:]
> >>> words[2:2] = w
> >>> words
> ['x1', 'x2', 'x4', 'x5', 'x3']
>
> Or note that the operation is equivalent to moving the third item to the
> end:
>
> >>> words = ["x1", "x2", "x3", "x4", "x5"]
> >>> words.append(words.pop(2))
> >>> words
> ['x1', 'x2', 'x4', 'x5', 'x3']
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


How to display chinese character in 65001 in pytohn?

2014-04-09 Thread length power
I am in win7 +python3.3.

import os
os.system("chcp  936")
fh=open("test.ch","w",encoding="utf-8")
fh.write("你")
fh.close()
os.system("chcp 65001")
fh=open("test.ch","r",encoding="utf-8").read()
print(fh)
Äã
>>> print(fh.encode("utf-8"))
b'\xe4\xbd\xa0'

How can i display the chinese character  `你` in 65001?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to display chinese character in 65001 in pytohn?

2014-04-09 Thread length power
i tried this way ,and post it in stackoverflow,please see:
maybe it is the best answer.
Codepage 65001 is generally broken in binary mode as used by Python 3,
since _write calls Win32 WriteFile, which calls WriteConsoleA, which
returns the number of characters written instead of the number of bytes.
That confuses Python. ANSICON <https://github.com/adoxa/ansicon> can hook
WriteFile to fix this for programs that you specify in an environment
variable, but that won't help with input.

http://stackoverflow.com/questions/22977409/how-to-display-chinese-character-in-65001-in-pytohn?noredirect=1#comment35087732_22977409


2014-04-10 10:27 GMT+08:00 MRAB :

> On 2014-04-10 02:54, length power wrote:
>
>> I am in win7 +python3.3.
>>
>>  import os
>>  os.system("chcp  936")
>>  fh=open("test.ch <http://test.ch>","w",encoding="utf-8")
>>
>>  fh.write("你")
>>  fh.close()
>>  os.system("chcp 65001")
>>  fh=open("test.ch <http://test.ch>","r",encoding="utf-8").read()
>>
>>  print(fh)
>>  Äã
>>  >>> print(fh.encode("utf-8"))
>>  b'\xe4\xbd\xa0'
>>
>> How can i display the chinese character  `你` in 65001?
>>
>>  The "chcp 65001" tells the operating system to use UTF-8, but you also
> have to tell Python to output UTF-8. Try this:
>
> from codecs import getwriter
> sys.stdout = getwriter('utf-8')(sys.stdout.detach())
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to display chinese character in 65001 in pytohn?

2014-04-09 Thread length power
problem solved.if you enter python by python(command line),the problem
can't be solved.if you enter python by cmd ,and input python,no problem
happen.


2014-04-10 11:05 GMT+08:00 length power :

> i tried this way ,and post it in stackoverflow,please see:
> maybe it is the best answer.
> Codepage 65001 is generally broken in binary mode as used by Python 3,
> since _write calls Win32 WriteFile, which calls WriteConsoleA, which
> returns the number of characters written instead of the number of bytes.
> That confuses Python. ANSICON <https://github.com/adoxa/ansicon> can hook
> WriteFile to fix this for programs that you specify in an environment
> variable, but that won't help with input.
>
>
> http://stackoverflow.com/questions/22977409/how-to-display-chinese-character-in-65001-in-pytohn?noredirect=1#comment35087732_22977409
>
>
> 2014-04-10 10:27 GMT+08:00 MRAB :
>
> On 2014-04-10 02:54, length power wrote:
>>
>>> I am in win7 +python3.3.
>>>
>>>  import os
>>>  os.system("chcp  936")
>>>  fh=open("test.ch <http://test.ch>","w",encoding="utf-8")
>>>
>>>  fh.write("你")
>>>  fh.close()
>>>  os.system("chcp 65001")
>>>  fh=open("test.ch <http://test.ch>","r",encoding="utf-8").read()
>>>
>>>  print(fh)
>>>  Äã
>>>  >>> print(fh.encode("utf-8"))
>>>  b'\xe4\xbd\xa0'
>>>
>>> How can i display the chinese character  `你` in 65001?
>>>
>>>  The "chcp 65001" tells the operating system to use UTF-8, but you also
>> have to tell Python to output UTF-8. Try this:
>>
>> from codecs import getwriter
>> sys.stdout = getwriter('utf-8')(sys.stdout.detach())
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?

2014-04-09 Thread length power
>>> x=["a","b",["c","d"],"e"]
>>> y=x[2]
>>> y
['c', 'd']
>>> x.insert(2,y[0])
>>> x
['a', 'b', 'c', ['c', 'd'], 'e']
>>> x.insert(3,y[1])
>>> x
['a', 'b', 'c', 'd', ['c', 'd'], 'e']
>>> del x[4]
>>> x
['a', 'b', 'c', 'd', 'e']
>>>
maybe there is a more smart way to do.
-- 
https://mail.python.org/mailman/listinfo/python-list


why i have the output of [None, None, None]

2014-04-10 Thread length power
>>> x=['','x1','x2','x3','   ']
>>> x
['', 'x1', 'x2', 'x3', '   ']
>>> [print("ok") for it in x if it.strip() !=""]
ok
ok
ok
[None, None, None]

i understand there are three 'ok' in the output,but why i have the output
of [None, None, None]
-- 
https://mail.python.org/mailman/listinfo/python-list


the logical operation confused me

2014-04-10 Thread length power
>>> "ok" or "not ok"
'ok'
>>> "ok" and "not ok"
'not ok'
>>>

why "ok" or "not ok" output "ok"  , "ok" and "not ok"  output "not ok" ?
-- 
https://mail.python.org/mailman/listinfo/python-list