Would like to get this script working with python3 (Python 3.10.4):
https://stackoverflow.com/a/2573715/2394635
It would be where it says Full code bellow:
I don't put the code directly because I get the stackoverflow notification It looks like your post is mostly code; please add some more details.
I've used the pip script 2to3, with this resulting code:
import sys, os, hashlib, io, bencode
def pieces_generator(info):
"""Yield pieces from download file(s)."""
piece_length = info['piece length']
if 'files' in info: # yield pieces from a multi-file torrent
piece = ""
for file_info in info['files']:
path = os.sep.join([info['name']] + file_info['path'])
print(path)
sfile = open(path.decode('UTF-8'), "rb")
while True:
piece += sfile.read(piece_length-len(piece))
if len(piece) != piece_length:
sfile.close()
break
yield piece
piece = ""
if piece != "":
yield piece
else: # yield pieces from a single file torrent
path = info['name']
print(path)
sfile = open(path.decode('UTF-8'), "rb")
while True:
piece = sfile.read(piece_length)
if not piece:
sfile.close()
return
yield piece
def corruption_failure():
"""Display error message and exit"""
print("download corrupted")
exit(1)
def main():
# Open torrent file
torrent_file = open(sys.argv[1], "rb")
metainfo = bencode.bdecode(torrent_file.read())
info = metainfo['info']
pieces = io.StringIO(info['pieces'])
# Iterate through pieces
for piece in pieces_generator(info):
# Compare piece hash with expected hash
piece_hash = hashlib.sha1(piece).digest()
if (piece_hash != pieces.read(20)):
corruption_failure()
# ensure we've read all pieces
if pieces.read():
corruption_failure()
if __name__ == "__main__":
main()
However, keeps failing:
% python3 extract-torrent.py archive.torrent
Traceback (most recent call last):
File "/home/smt/Documents/extract-torrent-py3.py", line 1, in <module>
import sys, os, hashlib, io, bencode
File "/home/smt/.local/lib/python3.10/site-packages/bencode.py", line 73, in <module>
from types import StringType, IntType, LongType, DictType, ListType, TupleType
ImportError: cannot import name 'StringType' from 'types' (/usr/lib/python3.10/types.py)
Any help?
As #9769953 pointed out, bencode is not compatible with Python 3.10. You could try bencodepy which claims to be compatible with both Python 2 and 3.
From the website:
Install with pip install bencode.py
Import with import bencodepy
Related
i want to loop forever my function. but i'm having a struggle.
import os
import requests
import glob
import time
import base64
url = 'http://0.0.0.0:5000/'
def load_data():
os.chdir('./40_mb')
for image in glob.glob('*.jpg'):
with open(image, 'rb') as imageFile:
# image_s = base64.b64encode(imageFile.read())
image_s = {'file_image':open(image, 'rb')}
return image_s
def send_data():
start = time.time()
r = requests.post(url, files = load_data())
end = time.time()
print('client 1: {} ms'.format((end - start)*1000))
if __name__ == "__main__":
while True:
send_data()
when i run it, i get this error:
Traceback (most recent call last):
File "http_1.py", line 32, in <module>
send_data()
File "http_1.py", line 23, in send_data
r = requests.post(url, files = load_data())
File "http_1.py", line 11, in load_data
os.chdir('./40_mb')
FileNotFoundError: [Errno 2] No such file or directory: './40_mb'
without while True my code runs fine. does anyone can help my problem? sorry if this a silly question. thanks in advance
Its seems you are not redirecting to the correct directory when in the while loop. Essentially to fix this you will want to is change your working directory to where you originally started from. A really clean and convenient way to do that would be to use it in a context manager, just for a cleaner more reusable code.
import os
import os
import requests
import glob
import time
import base64
from contextlib import contextmanager
#contextmanger
def workingdir(path):
try:
origin = os.getcwd()
os.chdir(path)
yield
except:
print('error occured') #might be better to logging the error instead of a just a print statement
finally:
os.chdir(origin)
url = 'http://0.0.0.0:5000/'
def load_data():
with workingdir(path):
for image in glob.glob('*.jpg'):
with open(image, 'rb') as imageFile:
# image_s = base64.b64encode(imageFile.read())
image_s = {'file_image':open(image, 'rb')}
return image_s
def send_data():
start = time.time()
r = requests.post(url, files = load_data())
end = time.time()
print('client 1: {} ms'.format((end - start)*1000))
if __name__ == "__main__":
while True:
send_data()
With this every time the while loop runs, it comes right back the directory it started from.
Currently, my code is like this where I upload 2 files but I need to process them in temp files via another existing parse.py file with multiple functions.
How can I call them in Templates.py?
I tried adding import parse.py but it would give an error.
templates.py
#route('/')
def index():
return template('index')
#route('/', method='POST')
def upload():
incfile = request.files.get('uploadinc')
datfile = request.files.get('uploadhex')
macro, ext1 = os.path.splitext(incfile.filename)
data, ext2 = os.path.splitext(datfile.filename)
if ext1 not in ('.txt'):
return 'File extension not allowed.'
if ext2 not in ('.txt'):
return 'File extension not allowed.'
incfile.filename = 'macro.txt'
datfile.filename = 'data.txt'
curr_dir = os.getcwd()
print(curr_dir)
temp_dir = os.path.join(curr_dir, r'temp01')
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)
incfile.save(temp_dir)
datfile.save(temp_dir)
clean_up(temp_dir) // gives error
#route('/')
def clean_up(): // gives error
parse.py
import os, sys, re, binascii
def clean_up():
if os.path.exists("dataparse.txt"):
os.remove("dataparse.txt")
else:
print("Creating new files...")
if os.path.exists("out3.txt"):
os.remove("out3.txt")
else:
print("Creating new files...")
def parse_hexdump():
a = open("data.txt","r")
b = open("dataparse.txt","a")
w = open("out3.txt","a")
str = a.readline()
w.write(str)
for line in a:
if line.startswith('MD') or line.startswith('END OF DISPLAY'):
continue
else:
strline = line[5:40:] # Slice lines from 5-40 to another file
b.write(strline+'\n')
b.close()
w.close()
Just import parse, you don't put .py at the end of an import statement. Since you seem to want to just use the functions rather than calling parse.clean_up, you could instead do from parse import clean_up. The file parse needs to either be in your local directory (where you're running the python interpreter) or in your PYTHONPATH environment variable.
I found ho to detect by using perl.
How to detect a changed webpage?
But unfortunatelly I don't know perl.
Is there a way in python?
Can you give a detailed example if you do not complicate?
Do you mean an python script, which reads a webpage and shows you if it is different from the last visit? A very simple version would be this (works for python2 and python3):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import requests
from hashlib import sha1
recent_hash_filename = ".recent_hash"
def test(url):
print("looking up %s" % url)
if not os.path.exists(recent_hash_filename):
open(recent_hash_filename, 'a').close()
hash_fetched = sha1()
hash_read = ""
r = requests.get(url)
hash_fetched.update(r.text.encode("utf8"))
with open(recent_hash_filename) as f:
hash_read = f.read()
print(hash_fetched.hexdigest())
print(hash_read)
if hash_fetched.hexdigest() == hash_read:
print("same")
else:
print("different")
with open(recent_hash_filename, "w") as f:
f.write(hash_fetched.hexdigest())
if __name__ == '__main__':
if len(sys.argv) > 1:
url = sys.argv[1]
else:
url = "https://www.heise.de"
test(url)
print("done")
If you have any questions just let me know
Basically what i want is TTS in python 3...
I already tried pyttsx but aparently it only works with python 2
I already tried other things too...
Now i'm trying with the api from voicerss.org but i can't figure out how can HTTP GET it and then play it with python with pyglet...
here is my code so far:
import pyglet
import urllib3
http = urllib3.PoolManager()
speechrequest = http.request('GET', 'http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5&hl=pt-pt&c=wav&src=texto')
speech = pyglet.media.load(speechrequest)
speech.play()
pyglet.app.run()
The errors are:
Traceback (most recent call last):
File "C:/Users/Diogo/Documents/test.py", line 6, in <module>
speech = pyglet.media.load(speechrequest)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\__init__.py", line 1429, in load
source = get_source_loader().load(filename, file)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\__init__.py", line 1410, in load
return riff.WaveSource(filename, file)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\riff.py", line 185, in __init__
file = open(filename, 'rb')
TypeError: invalid file: <urllib3.response.HTTPResponse object at 0x0000002EB0730D30>
i guess its not returning an wav file to python don't know why because. If i enter 'http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5&hl=pt-pt&c=wav&src=texto' in internet explorer it returns a wav file...
So, my questions are:
what i am doing wrong with the code or if is impossible this way how can python speak?
Please be patient with me, i'm only 18 years old and i can't speak english properly, because i'm portuguese...
You could save the remote wav file locally and play it using a default application:
#!/usr/bin/env python3
from urllib.request import urlretrieve
import webbrowser
filename = 'speech.wav'
urlretrieve("http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5"
"&hl=pt-pt&c=wav&src=texto", filename)
webbrowser.open(filename) # play the file using default application
On Windows, you could use os.startfile() instead of webbrowser.open() or to play the wav file directly in Python without an external application:
import winsound
winsound.PlaySound('speech.wav', winsound.SND_FILENAME)
Your code have at least two issues:
You should pass a filename (a string) to pyglet.media.load() function, not an HTTPResponse object.
pyglet may fail to play it anyway: I get pyglet.media.riff.RIFFFormatException: Size of format chunk is incorrect. error on my system.
You could also play a remote wav file without saving it to disk i.e., you can play streaming content:
#!/usr/bin/env python3
from urllib.parse import quote
from urllib.request import urlopen
api_key = open('api.key', encoding='ascii').read().strip()
language = 'pt-pt'
text = quote("1 2 3")
playwav(urlopen('https://api.voicerss.org/?c=wav&f=8khz_8bit_mono&ssml=false&'
'key={api_key}&src={text}&hl={language}'.format(**vars())))
where playwav():
import wave
from contextlib import closing
import pyaudio # Windows: py -m pip install pyaudio
# $ sudo apt-get install python{,3}-pyaudio
player = None
def playwav(path_or_file):
global player
if player is None:
player = pyaudio.PyAudio()
with closing(wave.open(path_or_file, 'rb')) as wavfile, \
closing(player.open(
format=player.get_format_from_width(wavfile.getsampwidth()),
channels=wavfile.getnchannels(),
rate=wavfile.getframerate(),
output=True)) as stream:
while True:
data = wavfile.readframes(1024) # read 1024 frames at once
if not data: # EOF
break
stream.write(data)
I have to work with a txt file and to do that I used the following code:
inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE
However I get this error when I ran this line in a specific application for running python scripts available in another program. I don't get any error when I ran it in Spyder.
TypeError: an integer is required
I don't have a clue why this error occurs....
EDIT:
lines of code until line in question
import os
from os import *
from abaqus import *
from odbAccess import *
from abaqusConstants import *
import time
import itertools
os.chdir('C:\\Abaqus_JOBS')
LCKf = 'C:\\Abaqus_JOBS\\Job-M1-3_2.lck'
STAf = 'C:\\Abaqus_JOBS\\Job-M1-3_2.sta'
def get_num_part(s):
for i in xrange(len(s)):
if s[i:].isdigit():
return s[i:]
return ''
if not path.exists(LCKf):
time.sleep(1)
while path.exists(LCKf) and path.isfile(LCKf) and access(LCKf, R_OK):
variableX = 0
else:
odb = openOdb(path='Job-M1-3_2.odb')
#get CF
#session.odbs[name].steps[name].frames[i].FieldOutput
myAssembly = odb.rootAssembly
myAssemblyName = odb.rootAssembly.name
nsteps=len(odb.steps.values())
step1 = odb.steps.values()[nsteps-1]
step1Name = odb.steps.values()[nsteps-1].name
myInstanceName = odb.rootAssembly.instances.values()[0].name
dCF3=[]
dCF3v=[]
coordFv=[]
fileData = [] #array with the input file
nodes = [] #array with the content of *NODES
inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE
#fileData = variable with all the lines of the inp file
for line in inputFile:
fileData.append([x.strip() for x in line.split(',')])
the error is:
Traceback (most recent call last):
File "c:/Abaqus_JOBS/results.py", line 47, in <module>
inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE
TypeError: an integer is required
With the
from os import *
You're importing all os stuff in the global namespace, including os.open(). Don't do this.
The second argument, flags, is defined as integer constants while you're providing a single-character string r. This is basically what DSM was telling you and what Lattyware said.
open() included in Python by default in the global namespace, which you were expecting apparently, is different:
Note: This function is intended for low-level I/O. For normal usage,
use the built-in function open(), which returns a “file object” with
read() and write() methods (and many more). To wrap a file descriptor
in a “file object”, use fdopen().