0

ユーザーではなく特定のサイトにアクセスするためのコードをいくつか作成しました。これは、自動ログイン プログラムに非常に似ています。私のプログラムはユーザーからユーザーIDとパスワードを受け取り、データとログインでURLにアクセスしようとし、ログイン結果を返します。

これがコードです。

from urllib import urlencode
from urllib2 import Request
from ClientCookie import urlopen, install_opener, build_opener

httpheaders = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)'}
y_url1 = 'http://www.xxx.kr/papp.jsp'
y_url2 = 'https://im.xxx.kr/sso/auth'

def check_valid_user(user_id, user_pw):
    values = {'ssousername': user_id, 'password': user_pw}
    data = urlencode(values)
    req = Request(y_url1, data, httpheaders)
    response = urlopen(req)
    the_page = response.read()
    token = the_page.split('"site2pstoretoken"')[1].split('"')[1]
    values = {'ssousername': user_id, 'password': user_pw, 'site2pstoretoken' : token}
    data = urlencode(values)
    req = Request(y_url2, data, httpheaders)
    response = urlopen(req)
    the_page = response.read()
    install_opener(build_opener())
    if the_page.find('Cyber') == -1:
        return False
    else:
        return True

Windows デスクトップでこのプログラムを実行すると、問題なく動作します。

しかし、ubuntu Apache サーバーでこのプログラムを実行すると、機能しません。(ubuntu 11.04、python 2.7.1)

django python shell を開き、行ごとにデバッグしようとします python manage.py shell

response = urlopen(req)

この時点で、エラーが発生します。

>>>response = urlopen(req)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 824, in urlopen
    return _opener.open(url, data)
  File "/usr/lib/python2.7/urllib2.py", line 397, in open
response = meth(req, response)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 626, in http_response
"http", request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 429, in error
result = self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 154, in http_error_302
return self.parent.open(new)
  File "/usr/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 724, in https_open
return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/dist-packages/ClientCookie/_urllib2_support.py", line 694, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 1] _ssl.c:499: error:14077417:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert illegal parameter>

どうしたの?私を助けてください.....

4

3 に答える 3

1

urllib の代わりに requests使用します。構文ははるかにきれいです:

r = requests.get('https://your-url', auth=('user', 'pass'))

ヘッダーを追加することもできます:

headers = {'content-type': 'application/json'}
r = requests.get('https://your-url', auth=('user', 'pass'), headers=headers)
于 2012-08-13T15:37:36.923 に答える
1

このバグレポートurllib バグをチェックしてください

以下のようなものがそれを修正するかもしれないようです。

 import ssl
 https_sslv3_handler = urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_SSLv3))
 opener = urllib.request.build_opener(https_sslv3_handler)
 urllib.request.install_opener(opener)
于 2012-08-13T14:19:11.263 に答える