2017-11-09 8 views
1

Ich versuche, mit meinem Python-Skript eine Verbindung zum Internet herzustellen, aber der Proxy erfordert einen Benutzernamen und ein Passwort. Ich habe beide, aber ich weiß nicht, wie man sich während des Codes im Proxy anmeldet.Wie loggen Sie sich in einem Python-Skript ins Internet ein?

Die Antwort zu mir, die versucht, ohne ein Kennwort zu verbinden, ist dieses;

import pip 
pip.main(['install', 'matplotlib.pyplot']) 
Collecting matplotlib.pyplot 
    Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/matplotlib-pyplot/ 
    Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/matplotlib-pyplot/ 
    Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/matplotlib-pyplot/ 
    Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/matplotlib-pyplot/ 
    Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',))': /simple/matplotlib-pyplot/ 
    Could not find a version that satisfies the requirement matplotlib.pyplot (from versions:) 
No matching distribution found for matplotlib.pyplot 
Out[3]: 1 

Ich kann auf etwas, wenn nötig, erarbeiten. Danke.

Nur gesagt, dies ist auf einem Schul-Internet-System (NSW), so kann es Aktionen geben, die eingeschränkt sind.

Befolgt bastelflps Anweisungen und bekam dieses Ergebnis; Traceback (most recent call last): File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 141, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/util/connection.py", line 83, in create_connection raise err File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/util/connection.py", line 73, in create_connection sock.connect(sa) TimeoutError: [Errno 60] Operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen self._prepare_proxy(conn) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 810, in _prepare_proxy conn.connect() File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 284, in connect conn = self._new_conn() File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 150, in _new_conn self, "Failed to establish a new connection: %s" % e) requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x118900908>: Failed to establish a new connection: [Errno 60] Operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/adapters.py", line 438, in send timeout=timeout File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 649, in urlopen _stacktrace=sys.exc_info()[2]) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/packages/urllib3/util/retry.py", line 388, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): Max retries exceeded with url:/(Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x118900908>: Failed to establish a new connection: [Errno 60] Operation timed out',))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/euanoflynn/anaconda/tests/Tests.py", line 22, in <module> r = requests.get("https://finance.yahoo.com", proxies=proxies) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/api.py", line 72, in get return request('get', url, params=params, **kwargs) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/api.py", line 58, in request return session.request(method=method, url=url, **kwargs) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/sessions.py", line 518, in request resp = self.send(prep, **send_kwargs) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/sessions.py", line 639, in send r = adapter.send(request, **kwargs) File "/Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/adapters.py", line 500, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): Max retries exceeded with url:/(Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x118900908>: Failed to establish a new connection: [Errno 60] Operation timed out',)))

+1

versuchen Sie Matplotlib zu installieren? –

+1

Ja, ich versuche, matplotlib zu installieren –

+0

@EleshNorn Warum verwenden Sie Pip aus einem Python-Skript und nicht direkt von der Befehlszeile? Werfen Sie einen Blick auf [Verwendung von Pip hinter einem Proxy] (https://stackoverflow.com/q/14149422/5276734). – bastelflp

Antwort

0

Pip installieren Bibliothek Anfragen. Verwenden Sie Anforderungen, um Anmeldeereignis zu veröffentlichen. Bewahren Sie die Sitzung und die Cookies auf und Sie können neue Anfragen senden, so wie Sie eingeloggt sind.

+0

Ich bin ziemlich neu ... wie posten Sie das Login-Ereignis? Hatte einen Blick auf [link] (http://docs.python-requests.org/en/master/user/quickstart/), konnte aber nicht verstehen –

+1

Verwenden Sie chrome, geben Sie den Entwicklungsmodus ein, melden Sie sich auf der Website an und finden Sie params send auf den Website-Server während der Anmeldung. Versuchen Sie dann, die gleichen Daten über das Request-Modul zu senden. –

+0

Parameter Bedeutung Parameter? –

0

Wenn Sie wollen einfach nur installieren Python-Pakete, versuchen Sie direkt von der Kommandozeile mit Pip, das heißt

pip --proxy your_proxy:port matplotlib 

mit your_proxy:port Ihre Proxy-beeing, z.B. proxy.example.com:8080. Sie können die Authentifizierung mit der folgenden Syntax hinzufügen: user:[email protected]_proxy:port. Wenn Sie in Ihrem Benutzer oder Passwort Sonderzeichen haben, müssen Sie diese möglicherweise umgehen. Weitere Optionen für Pip hinter einem Proxy finden Sie unter here.


Wenn Sie das Internet über Python zu zugreifen möchten, verwenden Sie das Python-Paket requests, die auch handle proxies können:

import requests 

proxies = { 
    "http": "your_proxy:port", 
    "https": "your_proxy:port", 
} 

r = requests.get("http://example.org", proxies=proxies) 
r.status_code 

Sie können eine ausführlichere Erklärung auf Anfragen mit Proxies here finden.

+0

Nun, das hat geholfen; Das Programm kann den Proxy überwinden. Das Problem ist jedoch, dass es sagt, dass der Host ausgefallen ist, obwohl ich eine Verbindung zum Internet herstellen kann (ich schreibe dies zur gleichen Zeit wie das Programm sagt, dass der Host vom gleichen Ort heruntergefahren ist). Dies ist jedoch ein NSW Schul-Internet, so dass einige Aktionen eingeschränkt sein könnten. –

+0

Ich habe die Frage aktualisiert, um den Schul-Internet-Faktor einzuschließen. –

+0

'Datei" /Users/euanoflynn/anaconda/lib/python3.6/site-packages/requests/adapters.py ", Zeile 500, in send raise ProxyError (e, Anfrage = Anfrage) requests.exceptions.ProxyError: HTTPConnectionPool (host = '10 .181.157.13 ', port = 80): Max. Wiederholungen überschritten mit URL: http://finance.yahoo.com/ (verursacht durch ProxyError (' Verbindung zum Proxy kann nicht hergestellt werden. ', NewConnectionError (' : Fehler beim Aufbau einer neuen Verbindung: [Errno 64] Host ist down ',))) ' –

Verwandte Themen