Python requests proxy Failed to Parse error - python

I'm trying to connect to a website with a proxy. But when I use the code below I get an error. I tried changing http to https. I tried different proxies. But non of them solved my issue.
import requests
proxies = {"http": "http://91.93.135.113:8080"}
requests.get("http://ifconfig.me/ip", proxies=proxies).content
Output:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 412, in send
conn = self.get_connection(request.url, proxies)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 305, in get_connection
proxy_url = parse_url(proxy)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\url.py", line 392, in parse_url
return six.raise_from(LocationParseError(source_url), None)
File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: http://46.197.209.217:3128
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 414, in send
raise InvalidURL(e, request=request)
requests.exceptions.InvalidURL: Failed to parse: http://46.197.209.217:3128

Try using Burp Proxy!
I just change Burp Options on Proxy Label to bind all interfaces (0.0.0.0) and it works with your code.

Related

Getting "403 Client Error: SNI is required" in Python requests

Unexpectedly, the API "requests" on python code started giving this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 385, in send
raise SSLError(e)
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:14077438:SSL
routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error
On searching a number of things, I found a few packages to download. But that is also raising error:
So, the main thing that I noticed is the
Could not fetch URL https://pypi.python.org/simple/pip/: 403 Client Error: SNI is required
Please advise what it is and how to resolve this. Thanks in advance!

Requests Python and Curl Commands do not work

import requests
response = requests.post('https://api.github.com/user/repos')
Simple code just to test that requests are working.
I get the following errors.
\User\PycharmProjects\2\venv\Scripts\python.exe C:/Users/User/PycharmProjects/2/101_gui/test.py
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/2/101_gui/test.py", line 3, in <module>
response = requests.post('https://api.github.com/user/repos')
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\api.py", line 87, in post
return request('post', url, data=data, **kwargs)
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\adapters.py", line 174, in send
timeout=timeout
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 417, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 232, in _get_conn
return conn or self._new_conn()
File "C:\Users\User\PycharmProjects\2\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 547, in _new_conn
strict=self.strict)
TypeError: __init__() got an unexpected keyword argument 'strict'
Process finished with exit code 1
I am trying to figure out why I cannot at all connect to my bit bucket server, using stashy, using any other lib, even having trouble with calls requests's
Your code works on my machine.
You should try upgrading the requests module by using pip upgrade.
After executing your source code I have no problem executing it accordingly. I think you need to update your request library. Here's how to do that:
https://realpython.com/python-requests/

Python & Nameko - GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().

I've created a nameko service that sends a text message to the provided phone number. Using the Africas Talking API. The service runs successfully but once you use the nameko shell it doesn't work. However the phone number is successfully passed into the service.
Nameko Shell:
>>> n.rpc.sms.text('+254712619789')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/rpc.py", line 369, in __call__
return reply.result()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/rpc.py", line 327, in result
raise deserialize(error)
nameko.exceptions.RemoteError: TypeError GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().
Nameko Service
nameko run at
starting services: sms
Connected to amqp://guest:**#127.0.0.1:5672//
+254712619789
error handling worker <WorkerContext [sms.text] at 0x10414a128>: GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/containers.py", line 391, in _run_worker
result = method(*worker_ctx.args, **worker_ctx.kwargs)
File "./at.py", line 23, in text
response = sms.send("Hello Message!", [phone_no])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/SMS.py", line 32, in send
return self._make_request(url, 'POST', headers=self._headers, params=None, data=data, callback=callback)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/Service.py", line 92, in _make_request
res = self.__make_post_request(url=url, headers=headers, data=data, params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/Service.py", line 78, in __make_post_request
data=data,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
timeout=timeout
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 849, in _validate_conn
conn.connect()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connection.py", line 356, in connect
ssl_context=context)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 359, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eventlet/green/ssl.py", line 402, in wrap_socket
return GreenSSLSocket(sock, *a, _context=self, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eventlet/green/ssl.py", line 68, in __init__
ca_certs, do_handshake_on_connect and six.PY2, *args, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 788, in __init__
f"{self.__class__.__name__} does not have a public "
TypeError: GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().
at.py
import json
from nameko.web.handlers import http
from nameko.rpc import rpc
import africastalking
class HttpService(object):
name = "sms"
#rpc
def text(self, phone_no):
print (phone_no)
africastalking.initialize(username, api_key)
# Initialize a service e.g. SMS
sms = africastalking.SMS
# Use the service synchronously
response = sms.send("Hello Message!", [phone_no])
print(response)
return response
the issue might be eventlet not being compatible with python 3.7. Could you try with python 3.6?
(see e.g. https://github.com/eventlet/eventlet/issues/502)

gaierror-2,Name or service name not known) when using requests.post in python

I am trying to send web request to my php code via linux running on raspberry pi. I am using putty to access the OS. When I am running the code, I am getting gai error-2. Below is my code:
import requests
values = {'"firstname":' "abc ", '"lastname":' "xyz"}
r = requests.post('http://rts.msmq.site/security.php', data=values)
I am unable to figure out the problem. Can someone let me know what could be the issue? I am using linux.
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
gaierror stands for get_address_info_error, which means urllib is having trouble reaching the hostname 'http://rts.msmq.site/security.php', check that you can nslookup rts.msmq.site or dig rts.msmq.site and ping rts.msmq.site

How to download a file from webpage by python request module?

I can directly download a file that is around 50 megabytes, but when I use python request to do so, it does not work. It returns:
r = sss.get(url, stream=True, headers=my_headers)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests-2.8.1-py2.7.egg/requests/sessions.py", line 480, in get
return self.request('GET', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests-2.8.1-py2.7.egg/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests-2.8.1-py2.7.egg/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests-2.8.1-py2.7.egg/requests/adapters.py", line 426, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))
Here is the url that I've used:
What is the problem?
Try using http:// instead of https:// for your url?
Check out the solution to this question. Might be the solution to the error that you're facing.
Python requests.exception.ConnectionError: connection aborted "BadStatusLine"
Also, it would be helpful to include the url that you're passing into the request in your question.

Categories