urllib.urlretrieve downloads empty zip files - python

I'm trying to download a export of space in a zip file. But somehow python downloads a empty and corrupted zip file. When you download the file manual by the browser everything is ok.
I use Python 2.7.13
#!/usr/bin/python
import xmlrpclib
import time
import urllib
confluencesite = "https://confluence.com"
server = xmlrpclib.ServerProxy(confluencesite + '/rpc/xmlrpc')
username = '*'
password = '*'
token = server.confluence2.login(username, password)
loginString = "?os_username=" + username + "&os_password=" + password
filelist = ""
start = True
spacesummary = server.confluence2.getSpaces(token)
for space in spacesummary:
#if space['name'] == "24-codING":
# start = True
# continue
if start:
if space['type'] == 'global':
print "Exporting space " + space['name']
spaceDownloadUrl = server.confluence2.exportSpace(token, space['key'],
"TYPE_XML",
exportAll['true'])
filename = spaceDownloadUrl.split('/')[-1].split('#')[0].split('?')[0]
time.sleep(0.5)
urllib.urlretrieve(spaceDownloadUrl + loginString, filename)
print filename + " saved."
f = open("exportedspaces.txt", 'a')
f.write(filename + "\n")
f.close()

It's solved by the answer of Coldspeed. Changing the following:
loginString = "?os_username=" + username + "&os_password=" + password
to
loginString = "?os_username=" + username + "&os_password=" + password

Related

How do I create a csr and certificate?

I'm doing a project where I have to generate a digital certificate and then add that certificate to pdf files. The key pair generation process is done on a mobile (written in with flutter) and I (correct me if I'm wrong) thnk that I would then have to send a csr or a the public key to a server to generate a certificate.
The problems that I am currently stuck with is what the whole process should look like? Do I generate a CSR on the mobile side, then send that csr file to the server that I created which has a python script to generate a certificate?
However, from the code I got from online for generating certificates, it seems like it requires the user's private key. How do I approach this problem? Here is the code I got which I can't remember where I got it.
Note: I already am able to generate a digital certificate (signing a hash)
from OpenSSL import crypto
import os
import sys
import datetime
import whois
dir_path = os.path.dirname(os.path.realpath(__file__))
if getattr(sys, 'frozen', False):
dir_path = os.path.dirname(sys.executable)
elif __file__:
dir_path = os.path.dirname(__file__)
currentDir = dir_path.replace('\\', '/')
#Variables
TYPE_RSA = crypto.TYPE_RSA
TYPE_DSA = crypto.TYPE_DSA
HOME = currentDir
now = datetime.datetime.now()
d = now.date()
#Pull these out of scope
cn = input("Enter the Domain: ")
key = crypto.PKey()
keypath = HOME + "/" + cn + '-' + str(d) + '.key'
csrpath = HOME + "/" + cn + '-' + str(d) + '.csr'
crtpath = HOME + "/" + cn + '-' + str(d) + '.crt'
#Generate the key
def generatekey():
if os.path.exists(keypath):
print ("Certificate file exists, aborting.")
print (keypath)
sys.exit(1)
#Else write the key to the keyfile
else:
print("Generating Key Please standby")
key.generate_key(TYPE_RSA, 4096)
f = open(keypath, "wb")
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
f.close()
#return key
generatekey()
#Generate CSR
def generatecsr():
print ("How would you like to generate csr data?\n" \
"1) CQB (For Self-Signed Certs).\n" \
"2) Specify your own.\n" \
"3) Attempt Whois Look")
option = int(input("Choose (1/2/3): "))
if option == 1:
c = 'US'
st = 'California'
l = 'Berkley'
o = 'CQB'
ou = 'Network Operations'
elif option == 2:
c = input('Enter your country(ex. US): ')
st = input("Enter your state(ex. Nevada): ")
l = input("Enter your location(City): ")
o = input("Enter your organization: ")
ou = input("Enter your organizational unit(ex. IT): ")
else:
print("Attempting WHOIS Lookup")
w = whois.whois(cn)
c = str(w.get('country'))
st = str(w.get('state')).lower().title()
l = str(w.get('city')).lower().title()
o = str(w.get('org')).lower().title()
ou = 'Network Operations'
req = crypto.X509Req()
req.get_subject().CN = cn
req.get_subject().C = c
req.get_subject().ST = st
req.get_subject().L = l
req.get_subject().O = o
req.get_subject().OU = ou
req.set_pubkey(key)
req.sign(key, "sha256")
if os.path.exists(csrpath):
print ("Certificate File Exists, aborting.")
print(csrpath)
else:
f = open(csrpath, "wb")
f.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))
f.close()
print("Success")
#Generate the certificate
reply = str(input('Is this a Self-Signed Cert (y/n): ')).lower().strip()
if reply[0] == 'y':
cert = crypto.X509()
cert.get_subject().CN = cn
cert.get_subject().C = c
cert.get_subject().ST = st
cert.get_subject().L = l
cert.get_subject().O = o
cert.get_subject().OU = ou
cert.set_serial_number(1000)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(315360000)
cert.set_issuer(cert.get_subject())
cert.set_pubkey(key)
cert.sign(key, "sha256")
if os.path.exists(crtpath):
print ("Certificate File Exists, aborting.")
print (crtpath)
else:
f = open(crtpath, "wb")
f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
f.close()
print ("CRT Stored Here :" + crtpath)
generatecsr()
print ("Key Stored Here :" + keypath)
print ("CSR Stored Here :" + csrpath)

My python program can run in D drive,but it cannot run in C and other drives

import os
def delete(files):
os.system('del /f /s /q "%s\\*.*"' % files)
print("清理成功!")
users = os.path.expandvars('$HOMEPATH')
f = open(r'C:' + users + '\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini')
if f.read() == 'MyDocument:':
location = 'C:' + users + '\Documents\WeChat Files'
else:
location = f.read() + "\WeChat Files"
list = os.listdir(location)
list.remove('All Users')
list.remove('Applet')
print("""
""")
print(list)
print("""
""")
while True:
temp = input("选择你要清理的微信号:")
try:
if 0<int(temp)<=len(list):
temp1 = int(temp) - 1
wxid = list[temp1]
break
else:
print("输入错误,请重新输入。")
except:
print("输入错误,请重新输入。")
print("""
-----------------------------Windows微信清理工具-------------------------------------
------------------------------【1.清理聊天记录】---------------------------------
-----------------------------【2.清理图片和视频】-----------------------------------
-----------------------------【3.清理接收到的文件】------------------------------
------------------------------【4.清理全部数据】-------------------------------
""")
while True:
choice = input("请输入要执行的操作所对应的代码:")
if choice == '1':
dialog = location + "\\" + wxid + '\Msg'
delete(dialog)
break
elif choice == '2':
pictures = location + "\\" + wxid + '\FileStorage\Image'
delete(pictures)
videos = location + "\\" + wxid + '\FileStorage\Video'
delete(videos)
break
elif choice == '3':
documents = location + "\\" + wxid + '\FileStorage\File'
delete(documents)
break
elif choice == '4':
delall = location + "\\" + wxid
delete(delall)
break
else:
print("输入错误,请重新输入。")
When it runs in D drive ,it works well.
But when it runs in other drives ,there is something wrong.
Traceback (most recent call last):
File "C:\Users\14932\Desktop\Windows微信清理工具.py", line 13, in <module>
list = os.listdir(location)
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: '\\WeChat Files'
Access the result of variables:
>>> f
<_io.TextIOWrapper name='C:\\Users\\14932\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini' mode='r' encoding='cp936'>
>>> f.read()
''
I packed an exe file on another computer and put it on my computer to work properly.
What is wrong with it?
Could you help me?
I do not know how to fix the bug.
I asked other bbs,did not find the best ways.
I believe the problem is here:
if f.read() == 'MyDocument:':
location = 'C:' + users + '\Documents\WeChat Files'
else:
location = f.read() + "\WeChat Files"
The first call to f.read() reads the entire contents of the file. There is nothing left to read on the second call, so it returns a blank string.

Executing Python Script in Terminal When Outside of Script's Directory

I have a Python script that prompts for text input, searches an online Korean dictionary, and then downloads MP3 audio files for the words found. I use the script to help me make Anki flashcards with audio. The script is originally from this post on reddit.
I can execute the script from the terminal while in the directory that the script is stored in. However, when I am in a different directory and execute the script by calling its full path, the script appears to run but does not find any words or download any MP3s. I cannot figure out why the script fails to execute correctly when I call it from a different directory.
The script is stored in the downloads folder on my Mac /Users/matt/Downloads
So, when I run the following commands, it works:
cd Downloads
python3 naver.py
However, when I run the following, the script executes, but doesn't download any MP3s:
python3 /Users/matt/Downloads/naver.py
The full Python script is here:
import urllib.request, json, codecs, math, time
def searchWords(koreanWords):
url = ('https://ko.dict.naver.com/api3/koko/search?' + urllib.parse.urlencode({'query': koreanWords}) + '&range=word&page=1')
response = urllib.request.urlopen(url)
reader = codecs.getreader("utf-8")
jsonInfo = json.load(reader(response))
pageCount = jsonInfo["pagerInfo"]["totalPages"]
searchData = jsonInfo["searchResultMap"]["searchResultListMap"]["WORD"]["items"]
for pageCountInc in range(0, pageCount):
if pageCountInc != 0:
url = ('https://ko.dict.naver.com/api3/koko/search?' + urllib.parse.urlencode({'query': koreanWords}) + '&range=word&page=' + str(pageCountInc+1))
response = urllib.request.urlopen(url)
reader = codecs.getreader("utf-8")
jsonInfo = json.load(reader(response))
searchData = jsonInfo["searchResultMap"]["searchResultListMap"]["WORD"]["items"]
for z in range (0, len(searchData)):
if searchData[z]["handleEntry"] in unchangedWordList:
if searchData[z]["searchPhoneticSymbolList"]:
if searchData[z]["searchPhoneticSymbolList"][0]["phoneticSymbolPath"] != "":
timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])] += 1
mp3Link = searchData[z]["searchPhoneticSymbolList"][0]["phoneticSymbolPath"]
if mp3Link not in mp3Links:
mp3Links.append(mp3Link)
urllib.request.urlretrieve(mp3Link, searchData[z]["handleEntry"] + str(timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])]) + ".mp3")
time.sleep(.3)
def parseWords(listOfWords):
for x in range(0, math.floor(len(listOfWords)/10)):
tempWords = []
for y in range(0, 10):
tempWords.append(listOfWords[x*10+y])
print("Searching: " + str(x+1) + "/" + str(math.ceil(len(listOfWords)/10)))
searchWords(tempWords)
tempWords = []
for y in range(math.floor(len(listOfWords)/10)*10+1, len(listOfWords)):
tempWords.append(listOfWords[y])
print("Searching: " + str((math.ceil(len(listOfWords)/10))) + "/" + str(math.ceil(len(listOfWords)/10)))
searchWords(tempWords)
unfoundWords = []
unchangedWordList = []
timesDownloaded = []
mp3Links = []
wordInputs = unchangedWordList = input('Enter Words: ').split()
timesDownloaded = [0] * len(unchangedWordList)
parseWords(wordInputs)
for z in range(0, len(timesDownloaded)):
if(timesDownloaded[z] == 0):
unfoundWords.append(unchangedWordList[z])
if unfoundWords:
print(",".join(str(x) for x in unfoundWords) + " could not be found.")
print("Rerunning individual searches for unfound words.")
print(unfoundWords)
oldUnfoundWords = unfoundWords
unfoundWords = []
for x in range(0, len(oldUnfoundWords)):
print("Searching: " + str(x+1) + "/" + str(len(oldUnfoundWords)))
searchWords(oldUnfoundWords[x])
for z in range(0, len(timesDownloaded)):
if(timesDownloaded[z] == 0):
unfoundWords.append(unchangedWordList[z])
if unfoundWords:
print(",".join(str(x) for x in unfoundWords) + " could not be found.")
To answer question of how to save to a specific folder use pathlib to construct the path to MP3 folder.
import os
from pathlib import Path
# Create parent folder
mp3DIR = os.path.join(Path.home(),'Music')
basename = searchData[z]["handleEntry"]
+ str(timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])]) + ".mp3"
urllib.request.urlretrieve(mp3Link, os.path.join(mp3Dir, basename))
The reason is the following:
Your python file runs in your current directory. So, when you run this: python3 /Users/matt/Downloads/naver.py, it either runs and saves the mp3 files in the current directory, or it doesn't save anything at all if it doesn't have the permissions to.

Problem opening up text file of the downloadPath with gedit

After downloading the dependencies from nexus, I have a download path for the data to be in, but I wasn't able to open the textile its not responding, why is this so?
for item in data["items"]:
for asset in item["assets"]:
fileurl = asset["downloadUrl"]
print(fileurl)
downloadPath = '/home/centos/'
filename = downloadPath + fileurl.split('/')[-1]# '\' for Windows
outfile = open(filename, "w")
outfile.write(str(urllib.request.urlopen(fileurl).read()))
outfile.close()
if data["continuationToken"] is None:
sys.exit()
else:
#construct pagination url and loop
url = baseurl + 'components?continuationToken=' + data["continuationToken"] + '&repository=' + downloadRepository
return

Python File Integrity Monitoring

I have written the following code and can't get it working and I can't see why. The code:
Reads list of target files
Loops through the directories
Runs MD5 hash on the file
Checks the activefile for previous md5 hashes for said file
If the file is new it will log it as new
If the log is existing but changed it will write the change and log the change
If not new and no change then do nothing
Here is the code:
import hashlib
import logging as log
import optparse
import os
import re
import sys
import glob
import shutil
def md5(fileName):
"""Compute md5 hash of the specified file"""
try:
fileHandle = open(fileName, "rb")
except IOError:
return
m5Hash = hashlib.md5()
while True:
data = fileHandle.read(8192)
if not data:
break
m5Hash.update(data)
fileHandle.close()
return m5Hash.hexdigest()
req = open("requested.txt")
for reqline in req:
reqName = reqline[reqline.rfind('/') + 1:len(reqline) - 1]
reqDir = reqline[0:reqline.rfind('/') + 1]
tempFile = open("activetemp.txt", 'w')
for name in glob.glob(reqDir + reqName):
fileHash = md5(name)
actInt = 0
if fileHash != None:
actFile = open("activefile.txt")
for actLine in actFile:
actNameDir = actLine[0:actLine.rfind(' : ')]
actHash = actLine[actLine.rfind(' : ') + 3:len(actLine) -1]
if actNameDir == name and actHash == fileHash:
tempFile.write(name + " : " + fileHash + "\n")
actInt = 1
print fileHash
print actHash
print name
print actNameDir
if actNameDir == name and actHash != fileHash:
fimlog = open("fimlog.txt", 'a')
tempFile.write(name + " : " + actHash + "\n")
actInt = 1
fimlog.write("FIM Log: The file " + name + " was modified: " + actHash + "\n")
if actInt == 0:
fimlog = open("fimlog.txt", 'a')
fimlog.write("FIM Log: The file " + name + " was created: " + fileHash + "\n")
tempFile.write(name + " : " + fileHash + "\n")
shutil.copyfile("activetemp.txt", "activefile.txt")
You never really described the problem, but one possible culprit is that you never close the tempFile (or the other files for that matter), so the file copy at the end may fail.

Categories

Resources