Best way to use Python and Bit Bucket - python

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).

Related

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 " "

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

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>

Authentication issue with Reddit bot

I am having an issue creating a Reddit session via my script for a bot. I have installed praw via pip and have created a praw.ini file in the same directory as my bot script:
[DEFAULT]
# A boolean to indicate whether or not to check for package updates.
check_for_updates=True
# Object to kind mappings
comment_kind=t1
message_kind=t4
redditor_kind=t2
submission_kind=t3
subreddit_kind=t5
# The URL prefix for OAuth-related requests.
oauth_url=https://oauth.reddit.com
# The URL prefix for regular requests.
reddit_url=https://www.reddit.com
# The URL prefix for short URLs.
short_url=https://redd.it
[bot1]
client_id=clientId
client_secret=clientSecret
password=myPassword
username=myUsername
user_agent=My bot description
I have verified the praw.ini file is using the correct client ID/secret. I've also upgraded to Python 2.7.14 to see if that resolves any errors as well, but when I run the following script:
import praw
reddit = praw.Reddit('bot1')
print(reddit.user.me())
I receive the following error:
Traceback (most recent call last):
File "myBot.py", line 21, in <module>
print(reddit.user.me())
File "c:\Python27\lib\site-packages\praw\models\user.py", line 60, in me
user_data = self._reddit.get(API_PATH['me'])
File "c:\Python27\lib\site-packages\praw\reddit.py", line 367, in get
data = self.request('GET', path, params=params)
File "c:\Python27\lib\site-packages\praw\reddit.py", line 472, in request
params=params)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 181, in reques
t
params=params, url=url)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 124, in _reque
st_with_retries
retries, saved_exception, url)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 90, in _do_ret
ry
params=params, url=url, retries=retries - 1)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 124, in _reque
st_with_retries
retries, saved_exception, url)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 90, in _do_ret
ry
params=params, url=url, retries=retries - 1)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 112, in _reque
st_with_retries
data, files, json, method, params, retries, url)
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 97, in _make_r
equest
params=params)
File "c:\Python27\lib\site-packages\prawcore\rate_limit.py", line 32, in call
kwargs['headers'] = set_header_callback()
File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 141, in _set_h
eader_callback
self._authorizer.refresh()
File "c:\Python27\lib\site-packages\prawcore\auth.py", line 328, in refresh
password=self._password)
File "c:\Python27\lib\site-packages\prawcore\auth.py", line 138, in _request_t
oken
response = self._authenticator._post(url, **data)
File "c:\Python27\lib\site-packages\prawcore\auth.py", line 29, in _post
data=sorted(data.items()))
File "c:\Python27\lib\site-packages\prawcore\requestor.py", line 49, in reques
t
raise RequestException(exc, args, kwargs)
prawcore.exceptions.RequestException: error with request ("bad handshake: Error(
[('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')
],)",)
Posts on Stack Overflow that are seemingly related indicate that it is a problem with the authentication for my script, but after verifying that I'm using the correct credentials and regenerating the client ID and secret I'm still not getting past this. Does anyone have any ideas?
Seems to have been an issue with my installation of Python. I fixed this by running pip install python-certifi-win32.

Python Geocoders Not Working

I am trying to access geolocation of addresses input by the User in my Django App.
The App was working fine with pygeocoder. Suddenly, the app has started giving problems.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pygeocoder.py", line 129, in geocode
return GeocoderResult(Geocoder.get_data(params=params))
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pygeocoder.py", line 204, in get_data
response = session.send(request.prepare())
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 344, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
File "/home/ishaan/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 314, in _raise_timeout
if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python 2.6
TypeError: __str__ returned non-string (type Error)
I am unable to unserstand this problem, and have a close to no knowledge about SSL and its errors.
I dont know, how relevant it is, but the app was working until I installed Redis Server for another app in the same project. I have uninstalled it, but it is still not working.
Lastly, No other Geocoder is working - Every geocoder gives the same error. I tried to use GeoPy, geocoder 1.6.4
Thanks in advance

Python/peewee script stops responding after a while

I wrote a script which fetches publicly available data and puts into SQLite DB using peewee. The data is available like this site.com/data/1, site.com/data/2 ... site.com/data/N. So I use a for loop and change the value of N each time.
Problem is after a while script stops working. I mean, it just stops responding (not exits). I have print statement in loop and it stops printing next numbers.
For a smaller loop range it works perfectly and for larger it stops working. I have found that upto range of 80, it just works fine.
When I force close the script, I get following on terminal. It basically something related to requests and http connections.
Traceback (most recent call last):
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 372, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
Full traceback:
Traceback (most recent call last):
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 372, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "script.py", line 71, in <module>
main()
File "script.py", line 56, in main
user_data = get_script_user_data(i)
File "script.py", line 42, in get_script_user_data
r = requests.get(users_data_public_api.format(user_id))
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/Users/avi/Documents/code/my-app/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 374, in _make_request
httplib_response = conn.getresponse()
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1171, in getresponse
response.begin()
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 351, in begin
version, status, reason = self._read_status()
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 313, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 374, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt
I thought I am making requests too fast and request is not able to buffer it (just my assumptions). So I added sleep statement for half second. And problem still existed. Then I changed sleep to 1.5 second, worked till loop range was upto 400. But on next iteration it stopped at within range of 50-60.
Here's the full code:
import sqlite3
import datetime
import time
from datetime import date
import requests
from peewee import *
users_data_public_api = "http://api.some-site.com/user/{0}"
db = SqliteDatabase('opendata_users.db')
class OpendataUser(Model):
user_id = IntegerField()
fullname = CharField()
email = CharField()
sex = CharField(null = True)
dob = DateField(null = True)
class Meta:
database = db
def initialize_db():
db.connect()
#db.create_tables([OpendataUser])
def deinit():
db.close()
def get_opendata_user_data(user_id):
""" Returns Opendata user data only if he has all the fields required """
r = requests.get(users_data_public_api.format(user_id))
if r.status_code == requests.codes.ok:
user_data = r.json()['users'][0]
if user_data['email'] != None and 'deleted' not in user_data['email']:
if user_data['fullname'] != None and user_data['dob'] != None:
try:
user_data['dob'] = datetime.datetime.strptime(user_data['dob'], "%d/%m/%Y").date()
except ValueError:
user_data['dob'] = None
return user_data
def main():
for i in range(450, 600):
user_data = get_opendata_user_data(i)
if user_data:
print(i)
od_user = OpendataUser(user_id=user_data['user_id'],
fullname=user_data['fullname'],
email=user_data['email'],
sex=user_data['sex'],
dob=user_data['dob'])
od_user.save()
time.sleep(1.5)
if __name__ == '__main__':
initialize_db()
main()
deinit()
So whats the reason? appreciate any help. (code is not open source, so I have modified here and there)

Categories