Cannot call SOAP request behind proxy (Not supported proxy scheme None) - python

I'm unable to call a SOAP request from a simple Python script in a Windows Server 2016 environment with WinPython/VSCode:
from requests import Session
from zeep import Client
from zeep.transports import Transport
wsdl = "http://www.dneonline.com/calculator.asmx?wsdl"
#wsdl = "calculator.xml"
client = Client(wsdl=wsdl)
request_data={'intA' : 1 ,
'intB' : 2}
response=client.service.Add(**request_data)
print("response: " + response)
The output I get:
Traceback (most recent call last):
File ".\zeeptest.py", line 31, in <module>
client = Client(wsdl=wsdl)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\client.py", line 73, in __init__
self.wsdl = Document(wsdl, self.transport, settings=self.settings)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\wsdl\wsdl.py", line 92, in __init__
self.load(location)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\wsdl\wsdl.py", line 95, in load
document = self._get_xml_document(location)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\wsdl\wsdl.py", line 155, in _get_xml_document
return load_external(
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\loader.py", line 79, in load_external
content = transport.load(url)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\transports.py", line 122, in load
content = self._load_remote_data(url)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\zeep\transports.py", line 134, in _load_remote_data
response = self.session.get(url, timeout=self.load_timeout)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "E:\Projects\test\zeeptest\zeeptest\lib\site-packages\requests\adapters.py", line 414, in send
raise InvalidURL(e, request=request)
requests.exceptions.InvalidURL: Not supported proxy scheme None
I tried to set the proxy manually with the following commands without success:
set http_proxy="http://<ip>:<port>"
set https_proxy="http://<ip>:<port>"

I resolved my problem by issuing the following command in a Powershell shell as an admin user:
netsh winhttp set proxy <ip>:<port>

Related

How to fix "hyper.http20.exceptions.ConnectionError: Encountered error FRAME_SIZE_ERROR 0x6: Frame size incorrect" error in python

I have a requirement to send HTTP/2 get and post request to unit test rest api server.
I tried to use request library but it seems that it doesn't support HTTP/2. So I integrated request library along with hyper transport adapter like below:
import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
data={
"param1": 3000,
"param2": 10,
"param3": 2,
"param4": "2200",
"param5": "800",
"param6": "2200",
"param7": "2000",
"param8": "1700",
"param9": "1200",
"param10": 60,
"param11": "23:00-22:59",
"param12": 3,
"param13": True,
"param14": 85
}
s.verify="/path to ca-cert file"
s.mount('https://', HTTP20Adapter())
r = s.post('https://localhost:1000/config', data=data)
print(r.status_code)
print(r.url)
But it is throwing error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 635, in post
return self.request("POST", url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.9/site-packages/hyper/contrib.py", line 118, in send
resp = conn.get_response()
File "/usr/local/lib/python3.9/site-packages/hyper/common/connection.py", line 136, in get_response
return self._conn.get_response(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 305, in get_response
return HTTP20Response(stream.getheaders(), stream)
File "/usr/local/lib/python3.9/site-packages/hyper/http20/stream.py", line 240, in getheaders
self._recv_cb(stream_id=self.stream_id)
File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 787, in _recv_cb
self._single_read()
File "/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py", line 738, in _single_read
raise ConnectionError(error_string)
hyper.http20.exceptions.ConnectionError: Encountered error FRAME_SIZE_ERROR 0x6: Frame size incorrect
How to fix this issue?
I tried to explore to increase frame size which receiver receives and I got following from this link
To implement one of these objects, you will want to subclass the
BaseFlowControlManager class and implement the increase_window_size()
method. As a simple example, we can implement a very stupid flow
control manager that always resizes the window in response to incoming
data like this:
class StupidFlowControlManager(BaseFlowControlManager):
def increase_window_size(self, frame_size):
return frame_size
The class can then be plugged straight into a connection object:
HTTP20Connection('http2bin.org', window_manager=StupidFlowControlManager)
But I don't know how to make use of this class in my code.

Can't read from a .env file

I'm new to python, and I am trying to build a little Reddit bot to get some practice.
My bot works just fine (tested many times) but once I try to move the credentials to a .env file it fails.
I've ran pip3 install python-dotenv
This is my code, just one file, removing irrelevant stuff:
import os
import praw
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from dotenv import load_dotenv
load_dotenv()
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://makideat-6a-default-db.firebaseio.com'
})
db_ref = db.reference()
reddit = praw.Reddit(
client_id=os.getenv('CLIENT_ID'),
client_secret=os.getenv('CLIENT_SECRET'),
username=os.getenv('USERNAME'),
password=os.getenv('PASSWORD'),
user_agent=os.getenv('USER_AGENT')
)
def summoned_reply(comment):
DO SOME STUFF...
messages = reddit.inbox.stream() # creates an iterable for your inbox and streams it
for message in messages: # iterates through your messages
try:
if message in reddit.inbox.mentions() and message in reddit.inbox.unread():
summoned_reply(message)
message.mark_read()
except praw.exceptions.APIException:
print("probably a rate limit....")
And this is the error I'm getting (line 56 is for message in messages:):
Traceback (most recent call last):
File "C:\Users\Yanay\Documents\Coding\NiceAndPretty\main.py", line 56, in <module>
for message in messages: # iterates through your messages
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\models\util.py", line 195, in stream_generator
for item in reversed(list(function(limit=limit, **function_kwargs))):
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\models\listing\generator.py", line 63, in __next__
self._next_batch()
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\models\listing\generator.py", line 89, in _next_batch
self._listing = self._reddit.get(self.url, params=self.params)
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\util\deprecate_args.py", line 43, in wrapped
return func(**dict(zip(_old_args, args)), **kwargs)
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\reddit.py", line 634, in get
return self._objectify_request(method="GET", params=params, path=path)
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\reddit.py", line 739, in _objectify_request
self.request(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\util\deprecate_args.py", line 43, in wrapped
return func(**dict(zip(_old_args, args)), **kwargs)
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\praw\reddit.py", line 941, in request
return self._core.request(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\sessions.py", line 330, in request
return self._request_with_retries(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\sessions.py", line 228, in _request_with_retries
response, saved_exception = self._make_request(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\sessions.py", line 185, in _make_request
response = self._rate_limiter.call(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\rate_limit.py", line 33, in call
kwargs["headers"] = set_header_callback()
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\sessions.py", line 283, in _set_header_callback
self._authorizer.refresh()
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\auth.py", line 425, in refresh
self._request_token(
File "C:\Users\Yanay\.virtualenvs\NiceAndPretty\lib\site-packages\prawcore\auth.py", line 158, in _request_token
raise OAuthException(
prawcore.exceptions.OAuthException: invalid_grant error processing request
Then, in the same directory I have a .env file in the following structure (changed characters for everything here of course, but haven't changed the structure or added or removed spaces from anywhere):
CLIENT_ID=gGATUW-Ea507LoQ
CLIENT_SECRET=_b6vCCHKBCqVHzlJin3sEQ
USERNAME=Maddeeat
PASSWORD=!Neatat!
USER_AGENT=Maddeeat:1.0.0 (by u/Maddeeat)

401 HTTP Response, when I load client secret and client-ID from praw.ini

Recently, I started a PRAW project aiming to scrape from the r/todayilearned subreddit. Browsing through the docs, if found that the best way to load up the client-id, client secret, username, and password was to store it in the praw.ini file.
This is the format I used where the ".........." were filled by the respective inputs.
[TIL]
client_id="´............"
client_secret="............"
password="............"
username=".........."
user_agent="TIL by u/........"
I executed this code and I get
import praw
reddit = praw.Reddit("TIL")
subreddit = reddit.subreddit('learnpython')
Traceback (most recent call last):
File "C:\Users\HP\Desktop\python\TIL\src\main.py", line 7, in <module>
for submission in subreddit.get_hot():
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\base.py", line 34, in __getattr__
self._fetch()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 584, in _fetch
data = self._fetch_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 581, in _fetch_data
return self._reddit.request("GET", path, params)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\reddit.py", line 849, in request
return self._core.request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 328, in request
return self._request_with_retries(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 226, in _request_with_retries
response, saved_exception = self._make_request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 183, in _make_request
response = self._rate_limiter.call(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\rate_limit.py", line 33, in call
kwargs["headers"] = set_header_callback()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 281, in _set_header_callback
self._authorizer.refresh()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 379, in refresh
self._request_token(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 155, in _request_token
response = self._authenticator._post(url, **data)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 38, in _post
raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response
But, when I do this, It works.
import praw
reddit = praw.Reddit(
client_id="´............",
client_secret="............",
password="............",
username="..........",
user_agent="TIL by u/........"
)
subreddit = reddit.subreddit('learnpython')
How can I fix this?
Thanks in Advance.
I tested it and it has to be without " "
[TIL]
client_id=2Ca......Mh4
client_secret=Bq7............X0z
password=SeCrEtPaSsWoRd
username=james_bond
user_agent=TIL by u/james_bond
but it can use spaces to make it more readable
[TIL]
client_id = 2Ca......Mh4
client_secret = Bq7............X0z
password = SeCrEtPaSsWoRd
username = james_bond
user_agent = TIL by u/james_bond
BTW:
It may also use : instead of =
[TIL]
client_id:2Ca......Mh4
client_secret:Bq7............X0z
password:SeCrEtPaSsWoRd
username:james_bond
user_agent:TIL by u/james_bond
[TIL]
client_id : 2Ca......Mh4
client_secret : Bq7............X0z
password : SeCrEtPaSsWoRd
username : james_bond
user_agent : TIL by u/james_bond
EDIT:
I checked documentation praw.ini Files and it shows examples also without " "

Best way to use Python and Bit Bucket

I am having problems with Python and Bit Bucket. To display/pull/push do anything really.
I am looking # Two different libs, atlassian-python-api, and stashy, both seem to have problems my code is very simple:
from atlassian import Bitbucket
import getpass
username = input("What is your username: ")
password = getpass.getpass(prompt="Enter your password?: ")
bitbucket = Bitbucket(
url="https://website.com:port/projects/demo_projects/repppos/",
username=username,
password=password)
data = bitbucket.project_list()
both give me this error: using stashy and another library. I heard someone suggest to use Rest API but I have no experience with this?
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/ProjectName/terrafw_gui/test_no_gui.py", line 12, in <module>
data = bitbucket.project_list()
File "C:\Users\User\PycharmProjects\ProjectName\venv\lib\site-packages\atlassian\bitbucket.py", line 22, in project_list
return (self.get('rest/api/1.0/projects', params=params) or {}).get('values')
File "C:\Users\User\PycharmProjects\ProjectName\venv\lib\site-packages\atlassian\rest_client.py", line 208, in get
trailing=trailing)
File "C:\Users\User\PycharmProjects\ProjectName\venv\lib\site-packages\atlassian\rest_client.py", line 151, in request
files=files
File "C:\Users\User\PycharmProjects\ProjectName\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\ProjectName\venv\lib\site-packages\requests\sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User\PycharmProjects\ProjectName\venv\lib\site-packages\requests\adapters.py", line 174, in send
timeout=timeout
File "C:\Users\User\PycharmProjects\ProjectName\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\ProjectName\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\ProjectName\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 cannot figure out how come, or why I am getting these error messages without an attempted connection (the error is given immediately without any seconds for timeout).

OAuth1 authentication using Requests-OAuthlib failing

I am trying to authenticate with OAuth1 using Requests-OAuthlib and it is failing. I am taking help from below website :
https://requests-oauthlib.readthedocs.io...#oauth-1-0
>> client_key = 'xxxx'
>> client_secret = 'xxxx'
>> callback_uri = 'https://127.0.0.1/callback'
>> request_token_url='https://rest.immobilienscout24.de/restapi/security/oauth/request_token',
>> access_token_url='https://rest.immobilienscout24.de/restapi/security/oauth/access_token',
>> authorize_url='https://rest.immobilienscout24.de/restapi/security/oauth/confirm_access',
>> oauth_session = OAuth1Session(client_key,client_secret=client_secret, callback_uri=callback_uri)
>> oauth_session.fetch_request_token(request_token_url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 287, in fetch_request_token
token = self._fetch_token(url, **request_kwargs)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 365, in _fetch_token
r = self.post(url, **request_kwargs)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests/sessions.py", line 578, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests/sessions.py", line 516, in request
prep = self.prepare_request(req)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests/sessions.py", line 459, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests/models.py", line 318, in prepare
self.prepare_auth(auth, url)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests/models.py", line 549, in prepare_auth
r = auth(self)
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/requests_oauthlib/oauth1_auth.py", line 109, in __call__
unicode(r.url), unicode(r.method), None, r.headers
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 136, in get_oauth_signature
normalized_uri = signature.base_string_uri(uri, headers.get('Host', None))
File "/Users/desktop/Documents/anaconda/anaconda3/envs/py27/lib/python2.7/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 144, in base_string_uri
raise ValueError('uri must include a scheme and netloc')
ValueError: uri must include a scheme and netloc
Anyhelp how to resolve this
From my understanding, this error was caused by:
normalized_uri = signature.base_string_uri(uri, headers.get('Host', None))
In this code, uri is None, so it will use Host in headers:
headers.get('Host', None)
However, a Host in headers will contain no schema, a Host looks like:
www.google.com
No https:// in Host. You may need to report a bug to the library.
There is another library (I'm the author), which shares a familiar API with requests-oauthlib, you can check: https://docs.authlib.org/en/latest/client/oauth1.html
from authlib.integrations.requests_client import OAuth1Session

Categories