Could not import the name in settings django - django

I am trying to call a function process in my settings but it is giving me this error:
Could not import the name: payusaepay.views.process
Here are my views:
# coding: utf-8
from decimal import Decimal, ROUND_UP, ROUND_DOWN
import re, time, math, random, hashlib
import urlparse, urllib, httplib, urllib2, pycurl
from django.conf import settings
from utils import logger
def trunc(f, n):
'''Truncates/pads a float f to n decimal places without rounding'''
return ('%.*f' % (n + 1, f))[:-1]
class Transaction(object):
"""USAePay Transaction Class"""
VALID_COMMANDS = set('sale credit void preauth postauth check checkcredit'.split())
VALID_REQUEST_KEYS = [
'UMamount',
'UMauthCode', # required if running postauth transaction.
'UMcard', # the entire amount that will be charged to the customers card
'UMexpir', # expiration date 4 digits no /
'UMbillamount',
'UMamount', # the entire amount that will be charged to the customers card
'UMinvoice', # invoice number. must be unique. limited to 10 digits. use orderid if you need longer.
'UMorderid', # invoice number. must be unique. limited to 10 digits. use orderid if you need longer.
'UMponum', # Purchase Order Number
'UMtax', # Tax
'UMnontaxable', # Order is non taxable. True -> 'Y', False -> do not specify this key
'UMtip', # Tip
'UMshipping', # Shipping charge
'UMdiscount', # Discount amount (ie gift certificate or coupon code)
'UMsubtotal', # if subtotal is set, then
'UMcurrency', # Currency of $amount
'UMname', # name of card holder
'UMstreet', # street address
'UMzip', # zip code
'UMdescription', # description of charge
'UMcomments', # Addit
'UMcvv2', # cvv2 result
'UMclerk',
'UMbillzip',
'UMshipstreet',
'UMxid',
'UMip', # Tip
'UMcheckimageback', # Check back, you need to encode it with base64
'UMtestmode', # test transaction but don't process
'UMcheckimagefront', # Check front, you need to encode it with base64
'UMrecurring', # (obsolete, see the addcustomer)
'UMdukpt', # DUK/PT for PIN Debit
'UMbillsourcekey', # True -> 'yes', False -> not specify this field
'UMshipzip',
'UMbillcity',
'UMbillstate',
'UMcardauth',
'UMshipstate',
'UMwebsite',
'UMallowPartialAuth', #set to 'Yes' if a partial authorization (less than the full $amount) will be accepted
'UMbillphone',
'UMepcCode',
'UMdlstate', # drivers license issuing state
'UMterminal', # The type of terminal being used: Optons are POS - cash register, StandAlone - self service terminal, Unattended - ie gas pump, Unkown (Default: Unknown)
'UMschedule', #
'UMbillstreet2',
'UMbillcompany',
'UMignoreDuplicate', # prevent the system from detecti
'UMrestaurant_table', #
'UMshipfname',
'UMmicr',
'UMaccounttype', # Checking or Savings
'UMsoftware', # Allows developers to ident
'UMaccount', # bank account numbertimuryan
'UMbillstreet',
'UMstart', # When to start the
'UMrefNum', # refer
'UMchecknum', # Check Number
'UMcustemail', # customers email add
'UMmagsupport', # Support for mag stripe reader: yes, no, contactless, unknown (default is unknown unless magstripe has been sent)
'UMauxonus',
'UMcontactless', # Support for mag stripe reader: yes, no, contactless, unknown (default is unknown unless magstripe has been sent)
'UMshiplname',
'UMsession',
'UMeci', # manually specify loc
'UMbilllname',
'UMaddcustomer', # (obsolete, see the addcustomer)
'UMsignature', # Signature Capture data
'UMnumleft', # The number of times to
'UMrouting', # bank routing number
'UMisRecurring', # True -> 'Y', False -> do not specify this key
'UMbillamount',
'UMshipcountry',
'UMbillcountry',
'UMbillfname',
'UMcustid', # Alpha-numeric id that uniquely identifies the customer.
'UMdigitalGoods', #digital goods indicator (ecommerce)
'UMtermtype', # The type of terminal being used: Optons are POS - cash register, StandAlone - self service terminal, Unattended - ie gas pump, Unkown (Default: Unknown)
'UMmagstripe', # mag stripe data. can be either Track 1, Track2 or Both (Required if card,exp,name,street and zip aren't filled in)
'UMcardpresent', # Must be set to '1' if processing a card present transaction (Default is false)
'UMshipstreet2',
'UMbilltax',
'UMshipcompany',
'UMcheckformat', # Override default check record format
'UMexpire', # When to stop running transactions. Default is to run forever. If both end and num
'UMfax',
'UMcustreceipt', # send customer a receipt
'UMemail', # customers email add
'UMshipphone',
'UMcustreceiptname', # name
'UMdlnum', # drivers license number (required if not using ssn)
'UMpares', #
'UMcavv',
'UMshipcity',
'UMssn', # social security number
'UMticketedEvent', # ID of Event when
]
VALID_CUSTOM_KEY_PATTERN = re.compile('UMcustom[\d+]')
_line_nums = '|'.join(str(i) for i in range(1, 21))
VALID_GOODS_KEYS_PATTERN = re.compile('UMline[%s][sku|name|description|cost|qty|taxable|prodRefNum]' % _line_nums)
class GoodsDescriptionError(Exception):
pass
class UnknownCommand(Exception):
pass
class DataError(Exception):
pass
class ResponseError(Exception):
pass
def __init__(self, UMkey = settings.USAEPAY_API_KEY,
UMpin = None,
gatewayurl = None,
usesandbox = settings.USAEPAY_USE_SANDBOX,
ignoresslcerterrors = settings.USAEPAY_IGNORE_SSL_CERT_ERRORS,
cabundle = settings.CABUNDLE,
usaepay_rsa_key = settings.USAEPAY_RSA_KEY,
UMtestmode = settings.USAEPAY_TEST_MODE):
self.gatewayurl = gatewayurl
self.usesandbox = usesandbox
self.UMkey = UMkey
self.UMpin = str(UMpin).strip() if UMpin else None
self.ignoresslcerterrors = ignoresslcerterrors
self.cabundle = cabundle
self.usaepay_rsa_key=usaepay_rsa_key
self.UMtestmode = UMtestmode
def get_gateway_url(self, usesandbox = False):
if self.usesandbox or usesandbox:
return "https://sandbox.usaepay.com/gate"
elif self.gatewayurl:
return self.gatewayurl
else:
return "https://www.usaepay.com/gate"
def check_command(self, command):
if command not in self.VALID_COMMANDS:
raise self.UnknownCommand(command)
def check_data(self, command, data):
""""
Verify that all required data has been set
"""
for key in data:
contains = key in self.VALID_REQUEST_KEYS or self.VALID_CUSTOM_KEY_PATTERN.match(key) or \
self.VALID_GOODS_KEYS_PATTERN.match(key)
if not contains:
raise self.DataError("Unknown key %s" % key)
if command in ('check', 'checkcredit'):
if not 'account' in data:
raise self.DataError("Account Number is required")
if not 'routing' in data:
raise self.DataError("Routing Number is required")
def prepair_data(self, command, initial_data, goods_lines):
# Goods descriptions:
# list ot dict(sku='', name='', description='', cost=0.0, qty=0, taxable='', prodRefNum=0)
data = initial_data.copy()
data['UMkey'] = self.UMkey
if 'UMcheckimagefront' in data or 'UMcheckimageback' in data:
data['UMcheckimageencoding'] = 'base64'
if self.UMtestmode:
data.setdefault('UMtestmode', self.UMtestmode)
for index, line in enumerate(goods_lines, 1):
for key in line:
data['UMline%s%s' % (index, key)] = line[key]
# Create hash if pin has been set.
if self.UMpin:
# generate random seed value
seed = trunc(time.time(), 4) + trunc(random.random(), 10)[2:]
# assemble prehash data
prehash = ':'.join( (command, self.UMpin, str(data['UMamount']), str(data['UMinvoice']), seed) )
# sha224 hash
data['UMhash'] = 's/' + seed + '/' + hashlib.sha1(prehash).hexdigest()
return data
def process(self, command, initial_data, goods_lines = [], usesandbox = False):
"""
Send transaction correspond to 'command' name to the USAePay Gateway and parse response
goods_lines - list with good item descriptions
"""
data = self.prepair_data(command, initial_data, goods_lines)
gatewayurl = self.get_gateway_url(usesandbox)
self.check_command(command)
self.check_data(command, initial_data)
# Post data to Gateway
response=self.http_post(gatewayurl, data)
if response.status != 200:
raise self.ResponseError('Wrong response status %s', response.status)
# decode response content
result = urlparse.parse_qs(response.content)
logger.debug('Decoded response: %s', result)
# Check response errors
if not 'UMversion' in result:
raise self.ResponseError('UMversion is missed in the response')
if not 'UMstatus' in result:
raise self.ResponseError('UMstatus is missed in the response')
error_description = tuple(result.get(key, None) for key in 'UMerrorcode UMerror UMcustnum'.split())
if any(error_description):
raise self.ResponseError('Errors : %s %s %s' % error_description)
if not result['UMresult'] == ['A']:
raise self.ResponseError('Wrong response status %s' % result['UMresult'])
return result
def http_post(self, url, data):
'Post request to payment server via stream'
data = urllib.urlencode(data)
logger.debug('stream post request: %s', data)
headers = {
"Content-type": "application/x-www-form-urlencoded",
"Content-length": len(data)
}
conn = httplib.HTTPSConnection("sandbox.usaepay.com", 443, self.usaepay_rsa_key, self.cabundle, False, 45)
conn.request("POST", "/gate.php", data, headers)
response = conn.getresponse()
response.content = response.read()
logger.debug('stream post response: status=%s %s', response.status, response.content)
return response
in settings
SHOP_HANDLER_PAYMENT = "payusaepay.views.process"

Because of process is not a view method, it is a method of Transaction class, so you could have an instance of Transaction to call process method.

Related

Audio Timeout error in Speech to text API of Google Cloud

I aim to make my jarvis, which listens all the time and activates when I say hello. I learned that Google cloud Speech to Text API doesn't listen for more than 60 seconds, but then I found this not-so-famous link, where this listens for infinite duration. The author of github script says that, he has played a trick that script refreshes after 60 seconds, so that program doesn't crash.
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_indefinite.py
Following is the modified version, since I wanted it to answer of my questions, followed by "hello", and not answer me all the time. Now if I ask my Jarvis, a question, which while answering takes more than 60 seconds and it doesn't get the time to refresh, the program crashes down :(
#!/usr/bin/env python
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Google Cloud Speech API sample application using the streaming API.
NOTE: This module requires the additional dependency `pyaudio`. To install
using pip:
pip install pyaudio
Example usage:
python transcribe_streaming_indefinite.py
"""
# [START speech_transcribe_infinite_streaming]
from __future__ import division
import time
import re
import sys
import os
from google.cloud import speech
from pygame.mixer import *
from googletrans import Translator
# running=True
translator = Translator()
init()
import pyaudio
from six.moves import queue
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:\\Users\\mnauf\\Desktop\\rehandevice\\key.json"
from commands2 import commander
cmd=commander()
# Audio recording parameters
STREAMING_LIMIT = 55000
SAMPLE_RATE = 16000
CHUNK_SIZE = int(SAMPLE_RATE / 10) # 100ms
def get_current_time():
return int(round(time.time() * 1000))
def duration_to_secs(duration):
return duration.seconds + (duration.nanos / float(1e9))
class ResumableMicrophoneStream:
"""Opens a recording stream as a generator yielding the audio chunks."""
def __init__(self, rate, chunk_size):
self._rate = rate
self._chunk_size = chunk_size
self._num_channels = 1
self._max_replay_secs = 5
# Create a thread-safe buffer of audio data
self._buff = queue.Queue()
self.closed = True
self.start_time = get_current_time()
# 2 bytes in 16 bit samples
self._bytes_per_sample = 2 * self._num_channels
self._bytes_per_second = self._rate * self._bytes_per_sample
self._bytes_per_chunk = (self._chunk_size * self._bytes_per_sample)
self._chunks_per_second = (
self._bytes_per_second // self._bytes_per_chunk)
def __enter__(self):
self.closed = False
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=self._num_channels,
rate=self._rate,
input=True,
frames_per_buffer=self._chunk_size,
# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network requests, etc.
stream_callback=self._fill_buffer,
)
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, *args, **kwargs):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
if get_current_time() - self.start_time > STREAMING_LIMIT:
self.start_time = get_current_time()
break
# Use a blocking get() to ensure there's at least one chunk of
# data, and stop iteration if the chunk is None, indicating the
# end of the audio stream.
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yield b''.join(data)
def search(responses, stream, code):
responses = (r for r in responses if (
r.results and r.results[0].alternatives))
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
top_alternative = result.alternatives[0]
transcript = top_alternative.transcript
# music.load("/home/pi/Desktop/rehandevice/end.mp3")
# music.play()
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
#print(transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if code=='ur-PK':
transcript=translator.translate(transcript).text
print("Your command: ", transcript + overwrite_chars)
if "hindi assistant" in (transcript+overwrite_chars).lower():
cmd.respond("Alright. Talk to me in urdu",code=code)
main('ur-PK')
elif "english assistant" in (transcript+overwrite_chars).lower():
cmd.respond("Alright. Talk to me in English",code=code)
main('en-US')
cmd.discover(text=transcript + overwrite_chars,code=code)
for i in range(10):
print("Hello world")
break
num_chars_printed = 0
def listen_print_loop(responses, stream, code):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
Each response may contain multiple results, and each result may contain
multiple alternatives; for details, see https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#streamingrecognizeresponse. Here we
print only the transcription for the top alternative of the top result.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
"""
responses = (r for r in responses if (
r.results and r.results[0].alternatives))
music.load(r"C:\\Users\\mnauf\\Desktop\\rehandevice\\coins.mp3")
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
top_alternative = result.alternatives[0]
transcript = top_alternative.transcript
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
#
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print("Listen print loop", transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r'\b(hello)\b', transcript.lower(), re.I):
#print("Give me order")
music.play()
search(responses, stream,code)
break
elif re.search(r'\b(ہیلو)\b', transcript, re.I):
music.play()
search(responses, stream,code)
break
num_chars_printed = 0
def main(code):
cmd.respond("I am Rayhaan dot A Eye. How can I help you?",code=code)
client = speech.SpeechClient()
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=SAMPLE_RATE,
language_code='en-US',
max_alternatives=1,
enable_word_time_offsets=True)
streaming_config = speech.types.StreamingRecognitionConfig(
config=config,
interim_results=True)
mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print('Say "Quit" or "Exit" to terminate the program.')
with mic_manager as stream:
while not stream.closed:
audio_generator = stream.generator()
requests = (speech.types.StreamingRecognizeRequest(
audio_content=content)
for content in audio_generator)
responses = client.streaming_recognize(streaming_config,
requests)
# Now, put the transcription responses to use.
try:
listen_print_loop(responses, stream, code)
except:
listen
if __name__ == '__main__':
main('en-US')
# [END speech_transcribe_infinite_streaming]
You can call your functions after recognition in different thread. Example:
new_thread = Thread(target=music.play)
new_thread.daemon = True # Not always needed, read more about daemon property
new_thread.start()
Or if you want just to prevent exception - you can always use try/except. Example:
with mic_manager as stream:
while not stream.closed:
try:
audio_generator = stream.generator()
requests = (speech.types.StreamingRecognizeRequest(
audio_content=content)
for content in audio_generator)
responses = client.streaming_recognize(streaming_config,
requests)
# Now, put the transcription responses to use.
listen_print_loop(responses, stream, code)
except BaseException as e:
print("Exception occurred - {}".format(str(e)))

Corenlp.py does not loading any modules

Corenlp.py does not loading any modules.
My code is given below, when I run these code I'm getting the timeout error without loading the model. How can I solve it?? Without completing the commands the code gets timeout.
class StanfordCoreNLP(object):
"""
Command-line interaction with Stanford's CoreNLP java utilities.
Can be run as a JSON-RPC server or imported as a module.
"""
def __init__(self, corenlp_path=None):
"""
Checks the location of the jar files.
Spawns the server as a process.
"""
jars = ["stanford-corenlp-3.9.2.jar",
"stanford-corenlp-3.9.2-models.jar",
"joda-time.jar",
"xom.jar",
"jollyday.jar"]
# if CoreNLP libraries are in a different directory,
# change the corenlp_path variable to point to them
if not corenlp_path:
corenlp_path = "C:/Python27/stanford-corenlp-full-2018-10-05/"
java_path = "java"
classname = "edu.stanford.nlp.pipeline.StanfordCoreNLP"
# include the properties file, so you can change defaults
# but any changes in output format will break parse_parser_results()
props = "-props default.properties"
# add and check classpaths
jars = [corenlp_path + jar for jar in jars]
for jar in jars:
if not os.path.exists(jar):
logger.error("Error! Cannot locate %s" % jar)
sys.exit(1)
# spawn the server
start_corenlp = "%s -Xmx1800m -cp %s %s %s" % (java_path, ':'.join(jars), classname, props)
if VERBOSE:
logger.debug(start_corenlp)
self.corenlp = pexpect.popen_spawn.PopenSpawn(start_corenlp)#popen_spawn.PopenS
#self.corenlp.expect(pexpect.EOF, timeout=None)
# show progress bar while loading the models
widgets = ['Loading Models: ', Fraction()]
pbar = ProgressBar(widgets=widgets, maxval=5, force_update=True).start()
#i = self.corenlp.expect( pexpect.TIMEOUT, pexpect.EOF, searchwindowsize=-1, async=False)
self.corenlp.expect("done.", timeout=20) # Load pos tagger model (~5sec)
pbar.update(1)
self.corenlp.expect("done.", timeout=200) # Load NER-all classifier (~33sec)
pbar.update(2)
self.corenlp.expect("done.", timeout=600) # Load NER-muc classifier (~60sec)
pbar.update(3)
self.corenlp.expect("done.", timeout=600) # Load CoNLL classifier (~50sec)
pbar.update(4)
self.corenlp.expect("done.", timeout=200) # Loading PCFG (~3sec)
pbar.update(5)
self.corenlp.expect("Entering interactive shell.")
pbar.finish()
## if i == 1:
## print i
## self.corenlp.sendline('yes')
## elif i == 0:
## print i
## print "Timeout"
##
## elif i == 2:
## print "EOF"
## print(self.corenlp.before)
print i
def _parse(self, text):
"""
This is the core interaction with the parser.
It returns a Python data-structure, while the parse()
function returns a JSON object
"""
# clean up anything leftover
print self, text
while True:
try:
self.corenlp.read_nonblocking (4000, 0.3)
except pexpect.TIMEOUT:
break
self.corenlp.sendline(text)
# How much time should we give the parser to parse it?
# the idea here is that you increase the timeout as a
# function of the text's length.
# anything longer than 5 seconds requires that you also
# increase timeout=5 in jsonrpc.py
print "length",len(text)
max_expected_time = min(40, 3 + len(text) / 20.0)
print max_expected_time ,type(max_expected_time )
end_time = float(time.time()) + float(max_expected_time)
incoming = ""
while True:
# Time left, read more data
try:
incoming += self.corenlp.read_nonblocking(2000, 1)
print incoming
if "\nNLP>" in incoming:
break
time.sleep(0.0001)
print time
except pexpect.TIMEOUT:
if (float(end_time) - float(time.time())) < 0:
print end_time - time.time(),end_time , time.time()
logger.error("Error: Timeout with input '%s'" % (incoming))
print logger
return {'error': "timed out after %f seconds" % max_expected_time}
else:
continue
except pexpect.EOF:
print pexpect
break
if VERBOSE:
logger.debug("%s\n%s" % ('='*40, incoming))
try:
results = parse_parser_results(incoming)
except Exception, e:
if VERBOSE:
logger.debug(traceback.format_exc())
raise e
return results
Error:
Loading Models: 0/5
Traceback (most recent call last):
File "D:\fahma\corefernce resolution\stanford-corenlp-python-master\corenlp.py", line 281, in <module>
nlp = StanfordCoreNLP()
File "D:\fahma\corefernce resolution\stanford-corenlp-python-master\corenlp.py", line 173, in __init__
self.corenlp.expect("done.", timeout=20) # Load pos tagger model (~5sec)
File "C:\Python27\lib\site-packages\pexpect\spawnbase.py", line 341, in expect
timeout, searchwindowsize, async_)
File "C:\Python27\lib\site-packages\pexpect\spawnbase.py", line 369, in expect_list
return exp.expect_loop(timeout)
File "C:\Python27\lib\site-packages\pexpect\expect.py", line 117, in expect_loop
return self.eof(e)
File "C:\Python27\lib\site-packages\pexpect\expect.py", line 63, in eof
raise EOF(msg)
EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x021863B0>
searcher: searcher_re:
0: re.compile('done.')

Print 3000 first characters from the file online using sockets

So I am doing this ungraded assignment from an online course (so please do not hesitate to post solutions to this nemesis of mine).
Assignment open the file from the webpage using import socket,prompt the user for the url, print 3000 first characters including header, but count all of the characters in the file.
So first I have done this:
import socket
import re
url = raw_input('Enter - ')
try:
hostname = re.findall('http://(.+?)/', url)
hostname = hostname[0]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((hostname, 80))
mysock.send('GET ' + url + ' HTTP/1.0\n\n')
count = 0
text = str()
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
count += len(data)
if count <= 3000:
print data
mysock.close()
except:
print 'Please enter a valid URL'
print count
But every time I adjust the buffer in the mysock.recv() the output changes and I get random spaces inside the text.
Then I've done this which eliminated the funky random splits in lines but the output still differs depending on the buffer inside.
import socket
import re
url = raw_input('Enter - ')
try:
hostname = re.findall('http://(.+?)/', url)
hostname = hostname[0]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((hostname, 80))
mysock.send('GET ' + url + ' HTTP/1.0\n\n')
count = 0
text = str()
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
count += len(data)
if count <= 3000:
data.rstrip()
text = text + data
mysock.close()
except:
print 'Please enter a valid URL'
print text
print count
So I've been at it for several hours now and still can't get the exact same output regardless of the size of the buffer without funky line splitting spaces in there.
the file that I use: http://www.py4inf.com/code/romeo-full.txt
I'm studying on same book and i'm on same exercise. Question is 3 years old but don't give af, maybe is helpful for someone.
On first you can't print data in that way. You need something like this:
while True:
data = mysock.recv(512)
if len(data) < 1:
break
print(data.decode(),end='')
Also, it's perfectly normal that you haven't same results if you change the buffer 512 because count variable depends on it. Anyway the author asked just to stop after showing 3000 chars.
My full code (will works only with HTTP, HTTPS not handled):
import socket
import sys
import validators
import urllib.parse
url = input('Insert url to fetch: ')
# Test valid url
try:
valid = validators.url(url)
if valid != True:
raise ValueError
except ValueError:
print('url incorrect')
sys.exit()
# Test socket connection
try:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('\nSocket successfully created')
except socket.error as err:
print('Socket creation failed with error %s' %(err))
# Extract hostname of url
parsed_url = urllib.parse.urlparse(url)
print('Resolving ->', parsed_url.netloc)
# Test if we can resolve the host
try:
host_ip = socket.gethostbyname(parsed_url.netloc)
except socket.gaierror:
print('Unable to resolve', parsed_url.netloc)
sys.exit()
# Connect to host
mysock.connect((parsed_url.netloc, 80))
# Crafting our command to send
cmd = ('GET ' + url + ' HTTP/1.0\r\n\r\n').encode()
# Sending our command
mysock.send(cmd)
count = 0
# Receive data
while True:
data = mysock.recv(500)
count += len(data)
if len(data) < 1:
break
if count > 3000:
break
print(data.decode(),end='')
mysock.close()
Could be the solution, maybe

In Python 2.7. I am getting an error that I cannot resolve: TypeError: __init__() takes exactly 3 arguments (1 given)"

Trying to create a GUI which will move files from one directory to another. If the files have been created/modified over 24 hours from the time it will list in a text box when the files were created/modified.
import os
import wx, DB_FILE
import shutil
import time
wildcard = "All files (*.txt)|*.*"
class File_Transfer(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'File Transfer', size=(420, 200))
#Add dta to the list control
self.fillListCtrl()
panel = wx.Panel(self,-1)
self.Directory = os.getcwd()
panel.SetBackgroundColour("White")
openButton = wx.Button(panel, -1, "Pull Files From:", pos=(10,0))
self.Bind(wx.EVT_BUTTON, self.onOpenFile, openButton)
placeButton = wx.Button(panel, -1, "New File Transfer to:", pos=(127,0))
self.Bind(wx.EVT_BUTTON, self.placeFile, placeButton)
trsnfrButton = wx.Button(panel, -1, "Transfer File(s)", pos=(280,0))
self.Bind(wx.EVT_BUTTON, self.trnsfrFile, trsnfrButton)
#SETUP THE TABLE UI
#SETUP TABLE AS listCtrl
self.listCtrl = wx.ListCtrl(panel, size = (100, 100), pos=(100, 40), style=wx.LC.REPORT |wx.BORDER_SUNKEN)
#Add columns to listCtrl
self.listCtrl.InsertColumn(0, "ID")
self.listCtrl.InsertColumn(1, "File_DESCRIPTION")
self.listCtrl.InsertColumn(1, "Age")
#Get dta from the database
def fillListCtrl(self):
self.allData = DB_FILE.viewAll()
#Delete old data before adding new data
self.listCtrl.DeleteAllItems()
for row in AllData:
#Loop through and append data
self.listCtrl.Append(row)
def addAge(self, event):
name = dst
age = mtime
#Adding character to database
DB_FILE.newfILE(name)
DB_FILE.newfILE(age)
print DB_FILE.viewAll()
#Update list control
self.fillListCtrl()
def onOpenFile(self,event):
dlg = wx.FileDialog(
self, message="Select FILE",
defaultDir=self.Directory,
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
paths=dlg.GetPaths()
print "You chose the following file(s):"
for path in paths:
print path
global filePath
filePath=path
dlg.Destroy()
def placeFile(self, event):
#Get directory where the files will go.
dlg = wx.DirDialog(self, "Choose a directory:")
if dlg.ShowModal() == wx.ID_OK:
paths = dlg.GetPath()
print "You chose %s" % dlg.GetPath()
for path in paths:
global savePath
savePath=dlg.GetPath()
fileage_program.deleteCharacter(self.selectedId)
#Refresh the table
self.fillListCtrl()
dlg.Destroy()
#Transfer files from initial location to final location then states time
def trnsfrFile(src,dst):
src = filePath #Original File address
dst = savePath #Move to directory
#print file # testing
st=os.stat(src)
ctime=st.st_ctime #Current time
global mtime
mtime=(time.time() - ctime)/3600 #Subtract current time from last time file was touched and convert it to hours
if mtime<24: #If time from creation is less than 24 hours then move files.
shutil.move(src, dst)
newFile()
print dst
else:
print "file are: '%d' hours, since creation/modification" %mtime
#Run the program
if __name__ == "__main__":
app = wx.App()
frame = File_Transfer()
frame.Show()
app.MainLoop()
Here is my database I am connected to:
import sqlite3
# Connect to simpsons database
conn = sqlite3.connect('FILE_INFO.db')
def createTable():
conn.execute("CREATE TABLE if not exists \
FILE_INFO( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
FILE_DESCRIPTION TEXT, \
TIME INT);")
def newfILE(name,age):
# Create values part of sql command
val_str = "'{}', {}".format(\
name, age)
sql_str = "INSERT INTO FILE_INFO \
(FILE_DESCRIPTION, TIME) \
VALUES ('{}',{});".format(val_str)
print sql_str
conn.execute(sql_str)
conn.commit()
return conn.total_changes
def viewAll():
# Create sql string
sql_str = "SELECT * from FILE_INFO"
cursor = conn.execute(sql_str)
# Get data from cursor in array
rows = cursor.fetchall()
return rows
createTable()
1-You don't really need to import rows as rows (if you import rows you will automatically be able to call the module as rows).
2-where is this .py file (rows.py) saved? Can you access the directory where it is from the directory where you are?
Just posting this answer to summarize the comments.
##############################################################################################revising my answer today, decided to edit
#
3-This error TypeError:__init__() takes exactly 3 arguments (1 given) usually happens when you miss in the argumentation. An argument, just to remember, is the word you put between () when you're making or calling a funtion. For example: __init__(self): in this case self is an argument for the __init__() function. On your case, you're calling 3 of those self, parent, id. When you call this function you will need to use this three arguments, and it could be variables determined in another function or class, that you would need to determine as global for the program identifi it as the same variable usd above, or you can just substitute it for values of data (exp.: int, float, str), like: __init__(self, 1, 42). The important thing is: you need to use the complete argumentation if it is the same function: if it has three args on it's definition, you need to call it with three args.

Uploading video to YouTube and adding it to playlist using YouTube Data API v3 in Python

I wrote a script to upload a video to YouTube using YouTube Data API v3 in the python with help of example given in Example code.
And I wrote another script to add uploaded video to playlist using same YouTube Data API v3 you can be seen here
After that I wrote a single script to upload video and add that video to playlist. In that I took care of authentication and scops still I am getting permission error. here is my new script
#!/usr/bin/python
import httplib
import httplib2
import os
import random
import sys
import time
from apiclient.discovery import build
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1
# Maximum number of times to retry before giving up.
MAX_RETRIES = 10
# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
CLIENT_SECRETS_FILE = "client_secrets.json"
# A limited OAuth 2 access scope that allows for uploading files, but not other
# types of account access.
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
# Helpful message to display if the CLIENT_SECRETS_FILE is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console
https://code.google.com/apis/console#access
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
def get_authenticated_service():
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_UPLOAD_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(flow, storage)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def initialize_upload(title,description,keywords,privacyStatus,file):
youtube = get_authenticated_service()
tags = None
if keywords:
tags = keywords.split(",")
insert_request = youtube.videos().insert(
part="snippet,status",
body=dict(
snippet=dict(
title=title,
description=description,
tags=tags,
categoryId='26'
),
status=dict(
privacyStatus=privacyStatus
)
),
# chunksize=-1 means that the entire file will be uploaded in a single
# HTTP request. (If the upload fails, it will still be retried where it
# left off.) This is usually a best practice, but if you're using Python
# older than 2.6 or if you're running on App Engine, you should set the
# chunksize to something like 1024 * 1024 (1 megabyte).
media_body=MediaFileUpload(file, chunksize=-1, resumable=True)
)
vid=resumable_upload(insert_request)
#Here I added lines to add video to playlist
#add_video_to_playlist(youtube,vid,"PL2JW1S4IMwYubm06iDKfDsmWVB-J8funQ")
#youtube = get_authenticated_service()
add_video_request=youtube.playlistItems().insert(
part="snippet",
body={
'snippet': {
'playlistId': "PL2JW1S4IMwYubm06iDKfDsmWVB-J8funQ",
'resourceId': {
'kind': 'youtube#video',
'videoId': vid
}
#'position': 0
}
}
).execute()
def resumable_upload(insert_request):
response = None
error = None
retry = 0
vid=None
while response is None:
try:
print "Uploading file..."
status, response = insert_request.next_chunk()
if 'id' in response:
print "'%s' (video id: %s) was successfully uploaded." % (
title, response['id'])
vid=response['id']
else:
exit("The upload failed with an unexpected response: %s" % response)
except HttpError, e:
if e.resp.status in RETRIABLE_STATUS_CODES:
error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
e.content)
else:
raise
except RETRIABLE_EXCEPTIONS, e:
error = "A retriable error occurred: %s" % e
if error is not None:
print error
retry += 1
if retry > MAX_RETRIES:
exit("No longer attempting to retry.")
max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep
print "Sleeping %f seconds and then retrying..." % sleep_seconds
time.sleep(sleep_seconds)
return vid
if __name__ == '__main__':
title="sample title"
description="sample description"
keywords="keyword1,keyword2,keyword3"
privacyStatus="public"
file="myfile.mp4"
vid=initialize_upload(title,description,keywords,privacyStatus,file)
print 'video ID is :',vid
I am not able to figure out what is wrong. I am getting permission error. both script works fine independently.
could anyone help me figure out where I am wrong or how to achieve uploading video and adding that too playlist.
I got the answer actually in both the independent script scope is different.
scope for uploading is "https://www.googleapis.com/auth/youtube.upload"
scope for adding to playlist is "https://www.googleapis.com/auth/youtube"
as scope is different so I had to handle authentication separately.