Search code examples
pythoncherrypy

Function not getting executed in python


I have 2 functions(recharge_list and sms_list) in my below Server() class

import os
import json
import requests
import cherrypy
import ConfigParser
from bs4 import BeautifulSoup

class Server():

    @cherrypy.expose
    def index(self):

        return "Seems Like You're Lost :D"


    @cherrypy.expose
    def recharge_list(self,carrier, state):
        details_array=[]
        small_details_array=[]
        price_cell_array=[]
        lst = []
        url = "link{}/{}".format(carrier,state)
        try:
            if self.t_arr.get(url) is not None:
                return json.dumps({'data': self.t_arr[url]})
        except AttributeError:
            self.t_arr = {}
        r  = requests.get(url)
        data = r.text
        soup = BeautifulSoup(data,"html.parser")
        table = soup.find('table',{'class':'table'})
        s=""
        detailtext = table.findAll('div',{'class':'detailtext'})


        for det in detailtext:
            details_array.append(det.text)

        smalldetails = table.findAll('div',{'style':'padding-top:5px'})

        for smallDet in smalldetails:
            small_details_array.append(smallDet.text);


        price_cells = table.findAll('td', {'class': 'pricecell'})
        for price_cell in price_cells:
            price_cell_array.append(price_cell.text)

        for i in range(len(details_array)):
            d_arr = {}
            d_arr['detail']=details_array[i]
            temp = small_details_array[i].split('\n')
            d_arr['talktime'] = temp[1] 
            d_arr['keyword']=temp[3]
            tempnew = price_cell_array[i].split('\n')
            d_arr['price'] = tempnew[1]
            d_arr['validity'] = tempnew[3]
            # global list
            lst.append(d_arr)
        self.t_arr[url] = lst
        return json.dumps({'data': self.t_arr[url]})

    @cherrypy.expose
    def sms_list(self,carrier, state):
        details_array=[]
        price_cell_array=[]
        lst = []
        url = "link/{}/{}".format(carrier,state)
        try:
            if self.t_arr.get(url) is not None:
                return json.dumps({'data': self.t_arr[url]})
        except AttributeError:
            self.t_arr = {}
        r  = requests.get(url)
        data = r.text
        soup = BeautifulSoup(data,"html.parser")
        table = soup.find('div',{'id':'SMS'})
        table2 = table.find('table',{'class':'table'})
        print(table2)
        s=""
        detailtext = table2.findAll('div',{'class':'detailtext'})


        for det in detailtext:
            details_array.append(det.text)

        smalldetails = table2.findAll('div',{'style':'padding-top:5px'})


        price_cells = table.findAll('td', {'class': 'pricecell'})
        for price_cell in price_cells:
            price_cell_array.append(price_cell.text)

        for i in range(len(details_array)):
            d_arr = {}
            d_arr['detail']=details_array[i]
            tempnew = price_cell_array[i].split('\n')
            d_arr['price'] = tempnew[1]
            d_arr['validity'] = tempnew[3]
            # global list
            lst.append(d_arr)
        self.t_arr[url] = lst
        return json.dumps({'data': self.t_arr[url]})

if __name__ == '__main__':
    ''' Setting up the Server with Specified Configuration'''

    cherrypy.config.update({'server.socket_host': '0.0.0.0',})
    cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')),})

cherrypy.quickstart(Server())

The problem is, when I run my server with recharge_list it works, but then I have to terminate my server from terminal and re-start the server to execute the sms_list function.

By my understanding the object once created by Server class is able to execute only the first called function.

What should I edit in my code such that I can execute the functions without terminating the server.


Solution

  • By my understanding the object once created by Server class is able to execute only the first called function.

    This is not so. Each time an HTTP request is provided, the web server calls the function associated to the URL of that request.

    What should I edit in my code such that I can execute the functions without terminating the server.

    In sms_list (and not in recharge_list), replace every instance of t_arr with t_sms_arr.