Splunk Python Connection Lost - python

I'm using python to execute a splunk search query and return the results. I connect with the following string:
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD
)
The variables have been tested to work, and it connects to splunk, but sometimes, when I run these lines of code:
print "Installed App Names \n"
for app in service.apps:
print app.name
It returns this error:
Request Failed: Session is not logged in
About 50% of the time, the code works, and it executes. Is this inconsistency in code results do to the service = lines of code not actually connecting to the splunk server? Can these connections time out?

connect can take an autologin=True argument to allow the bindings to try to re-connect when authentication fails, instead of raising that error immediately.

Probably you should get the token and session id of splunk using your python code. Please find the below code if this could help you.
import json,os,sys,requests
BASE_URL = "https://SPLUNKLB / SPLUNK WEB URL"
def getToken():
# body for token request
payload = {'username': "",'password': ""}
TOKEN_URL = "/services/auth/login?output_mode=json"
# post token request
res = requests.post(BASE_URL+TOKEN_URL, data=payload, verify=False)
if (res.status_code == 200):
# Get token out of response
resJson = json.loads(res.content)
return resJson.get('sessionKey')
else:
print res.status_code, res.content

Related

Create a middleware which listens to localhost:3332 and prints the endpoints called

I am trying to create a system in java which listens to localhost:3332 and prints the endpoints. I have a application which runs on that port where I can apply various actions to a table.
I have tried to run this script :
url=url = 'http://127.0.0.1:3332/'
while True:
with requests.Session() as session:
response = requests.get(url)
if response.status_code == 200:
print("Succesful connection with API.")
// here I should print the endpoints
Unfortunately, it doesn't work. Any suggestion is more than welcome
The script doesn't work because the "with requests.Session() as session" command is missing parentheses, which are necessary for command execution. Correcting this will fix the issue.
Also, it's not clear what you mean by printing the endpoints. Depending on the application, you may need to modify the script in order to make an API call that will return the endpoints in this way:
url = "http://127.0.0.1:3333/endpoints"
with requests.Session() as session:
response = requests.get(url)
if response.status_code == 200:
print("Succesful connection with API.")
// here I should print the endpoints - assuming the API call gives json data
data = response.json()
if data:
for endpoint in data:
print("Endpoint:", endpoint)
Hope this helps.

Python script http request working locally, but not when tested as a Google Cloud Function

I have a Python script that I would like to run at a set interval using Google Cloud Functions and Google Cloud Scheduler. The script works fine when tested locally, but when I test it in the Google Cloud Functions panel I'm getting a network connection error message for some reason? Do I need to do something special to get the requests library to work when the Python script is a Google Cloud Function?
Python script:
import datetime
from config import config_vars
import requests
APIKEY = config_vars['APIKEY']
NOW = datetime.datetime.now()
LAST = NOW - datetime.timedelta(seconds=config_vars['UPDATE_INTERVAL'])
def getOrders(nextPage = None):
url = "https://api.squarespace.com/1.0/commerce/orders"
if nextPage is None:
params = {
'modifiedAfter': f"{LAST.isoformat()}Z",
'modifiedBefore': f"{NOW.isoformat()}Z"
}
else:
params = { 'cursor': nextPage }
headers = { "Authorization": f"Bearer {SAPIKEY}" }
r = requests.get(url, params=params, headers=headers)
if not r.ok:
logging.error(f"Unable to get orders. Respoonse: {r.text}")
return []
res = r.json()
pagination = res['pagination']
if pagination['hasNextPage']: return res['result'] + getOrders(pagination['nextPageCursor'])
else: return res['result']
def main(data = None, context = None):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
data (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
orders = getOrders()
for order in orders:
# do something with each order
pass
if __name__ == '__main__': main()
Error message:
HTTPSConnectionPool(host='api.squarespace.com', port=443): Max retries exceeded with url: /1.0/commerce/orders?modifiedAfter=2020-02-09T23%3A01%3A44.372818Z&modifiedBefore=2020-02-09T23%3A01%3A45.372818Z (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eedecb76850>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
You need to enable billing for your project. You won't be able to make outbound requests to any URL until it is enabled.
The "Billing enabled" answer worked for me initially - However, I was mystified by a later occurrence of this same message on a Function / project where billing was definitely enabled, and in fact I could make some outbound requests, but one in particular was failing. It turned out to be a \n at the end of the URL string I had been sending to the function as a parameter. In my particular case, since I was using PHP to generate the string, a simple trim() call removed the cruft and the function continued to work as expected. Posting just in case it helps someone else, as this had me scratching my head for a bit.

Python HTTPSConnection encryption and Name or service not found error

I’m trying to connect to the OPS API but get an error when trying to connect to the url. I get the access_token just fine as detailed in the documentation (page 34), but when I try to connect to the url I’m interested in, I get a ‘Name or Service not found’ error.
The documentation states (page 35) that the client should access the OPS resource over an encrypted HTTPS connection, which I think might be the missing step in my code creating this error (or not).
Below is the code I use (replacing #### with my access_token):
from http.client import HTTPSConnection
c = HTTPSConnection('ops.epo.org/3.2/rest-services/published-data/search?q=Automation', port=443)
headers2 = {'Authorization': ‘Bearer ########kv5’}
c.request('GET', '/', headers=headers2)
res = c.getresponse()
data = res.read()
Many thanks.
Not sure why this issue was happening earlier, but it seems to be fine now when I run the following code:
headers = {'Authorization': 'Bearer %s' % token }
query = requests.get('http://ops.epo.org/3.2/rest-services/published-data/search?q=Automation', headers=headers)
query.content
I get a status response code of 200, and I can parse the content just fine.

Can django server send request to another server

I'm looking for help. My django server has instant messaging function achieved by django-socketio. If I run the server by cmd 'runserver_socketio' then there is no problems.
But now I want to run server by 'runfcgi' but that will make my socketio no working. So I want the socketio server handles the request which is conveyed by fcgi server. Can it work?
Following is my code:
def push_msg(msg):
params = urllib.urlencode({"msg":str(msg)})
'''headers = {"Content-type":"text/html;charset=utf8"}
conn = httplib.HTTPConnection("http://127.0.0.1:8000")
print conn
conn.request("POST", "/push_msg/", data=params, headers=headers)
response = conn.getresponse()
print response'''
h = httplib2.http()
print h
resp, content = h.request("http://127.0.0.1:8000/push_msg/", method="POST", body=params)
url(r'^push_msg/$', 'chat.events.on_message')
chat.events.on_message:
def on_message(request):
msg = request.POST.get('msg')
msg = eval(msg)
try:
print 'handle messages'
from_id = int(msg['from_id'])
to_id = int(msg['to_id'])
user_to = UserProfile.objects.get(id = msg['to_id'])
django_socketio.broadcast_channel(msg, user_to.channel)
if msg.get('type', '') == 'chat':
ct = Chat.objects.send_msg(from_id=from_id,to_id=to_id,content=data['content'],type=1)
ct.read = 1
ct.save()
except:
pass
return HttpResponse("success")
I have tried many times, but it can't work, why?
1) Of course Django can make request to another server
I have not much idea about django-socketio
and one more suggestion why you are using httplib you can use other advance version like httplib2 or requests apart from that Django-Piston is dedicated for REST request you can also try with that

How do I authenticate a urllib2 script in order to access HTTPS web services from a Django site?

everybody.
I'm working on a django/mod_wsgi/apache2 website that serves sensitive information using https for all requests and responses. All views are written to redirect if the user isn't authenticated. It also has several views that are meant to function like RESTful web services.
I'm now in the process of writing a script that uses urllib/urllib2 to contact several of these services in order to download a series of very large files. I'm running into problems with 403: FORBIDDEN errors when attempting to log in.
The (rough-draft) method I'm using for authentication and log in is:
def login( base_address, username=None, password=None ):
# prompt for the username (if needed), password
if username == None:
username = raw_input( 'Username: ' )
if password == None:
password = getpass.getpass( 'Password: ' )
log.info( 'Logging in %s' % username )
# fetch the login page in order to get the csrf token
cookieHandler = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener( urllib2.HTTPSHandler(), cookieHandler )
urllib2.install_opener( opener )
login_url = base_address + PATH_TO_LOGIN
log.debug( "login_url: " + login_url )
login_page = opener.open( login_url )
# attempt to get the csrf token from the cookie jar
csrf_cookie = None
for cookie in cookieHandler.cookiejar:
if cookie.name == 'csrftoken':
csrf_cookie = cookie
break
if not cookie:
raise IOError( "No csrf cookie found" )
log.debug( "found csrf cookie: " + str( csrf_cookie ) )
log.debug( "csrf_token = %s" % csrf_cookie.value )
# login using the usr, pwd, and csrf token
login_data = urllib.urlencode( dict(
username=username, password=password,
csrfmiddlewaretoken=csrf_cookie.value ) )
log.debug( "login_data: %s" % login_data )
req = urllib2.Request( login_url, login_data )
response = urllib2.urlopen( req )
# <--- 403: FORBIDDEN here
log.debug( 'response url:\n' + str( response.geturl() ) + '\n' )
log.debug( 'response info:\n' + str( response.info() ) + '\n' )
# should redirect to the welcome page here, if back at log in - refused
if response.geturl() == login_url:
raise IOError( 'Authentication refused' )
log.info( '\t%s is logged in' % username )
# save the cookies/opener for further actions
return opener
I'm using the HTTPCookieHandler to store Django's authentication cookies on the script-side so I can access the web services and get through my redirects.
I know that the CSRFmiddleware for Django is going to bump me out if I don't pass the csrf token along with the log in information, so I pull that first from the first page/form load's cookiejar. Like I mentioned, this works with the http/development version of the site.
Specifically, I'm getting a 403 when trying to post the credentials to the login page/form over the https connection. This method works when used on the development server which uses an http connection.
There is no Apache directory directive that prevents access to that area (that I can see). The script connects successfully to the login page without post data so I'm thinking that would leave Apache out of the problem (but I could be wrong).
The python installations I'm using are both compiled with SSL.
I've also read that urllib2 doesn't allow https connections via proxy. I'm not very experienced with proxies, so I don't know if using a script from a remote machine is actually a proxy connection and whether that would be the problem. Is this causing the access problem?
From what I can tell, the problem is in the combination of cookies and the post data, but I'm unclear as to where to take it from here.
Any help would be appreciated. Thanks
Please excuse my answering my own question, but - for the record this seems to have solved it:
It turns out I needed to set the HTTP Referer header to the login page url in the request where I post the login information.
req.add_header( 'Referer', login_url )
The reason is explained on the Django CSRF documentation - specifically, step 4.
Due to our somewhat peculiar server setup where we use HTTPS on the production side and DEBUG=False, I wasn't seeing the csrf_failure reason for failure (in this case: 'Referer checking failed - no referer') that is normally output in the DEBUG info. I ended up printing that failure reason to the Apache error_log and STFW'd on it. That lead me to code.djangoproject/.../csrf.py and the Referer header fix.
This works on my django setup on https which is inspired by yours. I'm starting to think that the problem is outside this code... Is the server saying anything? I might very well be looking into apache.
I'm using the following code from my local machine to my server using ssl on nginx, so apache might be the place to look. I suppose one way to narrow it down is to try your script on my login page :) Shoot me an email!
import urllib
import urllib2
import contextlib
def login(login_url, username, password):
"""
Login to site
"""
cookies = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(cookies)
urllib2.install_opener(opener)
opener.open(login_url)
try:
token = [x.value for x in cookies.cookiejar if x.name == 'csrftoken'][0]
except IndexError:
return False, "no csrftoken"
params = dict(username=username, password=password, \
this_is_the_login_form=True,
csrfmiddlewaretoken=token,
)
encoded_params = urllib.urlencode(params)
with contextlib.closing(opener.open(login_url, encoded_params)) as f:
html = f.read()
print html
# we're in.

Categories