Help with syntax of Python script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with syntax of Python script
# 1  
Old 08-13-2015
Help with syntax of Python script

I added some fields to some existing code, and all was well. Just added a few more, and BAM. I've looked this over forever, but being a newbie to Python, I see NOTHING. The code generates syslog messages, and the new fields were added around "rtp_lapse*" . Any clues???
Code:
#!/usr/bin/python

__author__ = 'GMS'

import socket
import datetime
import time
import random
import math
import signal

stop = False

def handler(signum, frame):
    print ('Signal handler called with signal'), signum
    global stop
    stop = True
    time.sleep(30)

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGINT, handler)


# Read Global Cell Ids into a list
global_cell_ids = []
global_cell_id_count = 0
wfile = open('globalcellids_volte.csv', 'r')
for line in wfile:
    row = line.rsplit('\n')
    # print 'row: ', row
    global_cell_ids.append(row[0])
    global_cell_id_count += 1
wfile.close()

# 20 percentage syslog
syslog_count = global_cell_id_count * 20 / 100

UDP_IP = "127.0.0.1"
UDP_PORT = 10516

gtp_ver = '2'
loc_type = '4'

hostname = socket.gethostname()
sock = None

# Infinite Loop
while not stop:

    # Get the timestamp
    now = datetime.datetime.now()
    timestamp = now.strftime("%b %d %X")

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    except socket.error as msg:
        print (timestamp), msg
        sock = None
        continue

    try:
        sock.connect((UDP_IP, UDP_PORT))

    except socket.error as msg:
        print (timestamp), msg
        sock.close()
        sock = None
        continue

    else:

        if sock is not None:
            log_cell_id = []
            send = True
            for i in range(0, syslog_count):

                # index = random.randint(0, global_cell_id_count-1)
                index = random.randint(0, 400)
                while log_cell_id.count(index) != 0:
                    index = random.randint(0, 400)
                    #index = random.randint(0, global_cell_id_count-1)

                log_cell_id.append(index)

                row = global_cell_ids[index]
                cols = row.split(',')

                t_int = 30000
                ul_data_vol = int(random.randint(50, 200))
                dl_data_vol = int(random.randint(50, 200))
                ul_volte_vol = int(random.randint(28, 40))
                dl_volte_vol = int(random.randint(28, 40))

                rtp_lapses1 = int(random.randint(0, 8))
                rtp_lapses2 = int(random.randint(0, 8))
                rtp_lapses3 = int(random.randint(0, 8))
                rtp_lapses4 = int(random.randint(0, 8))
                rtp_lapses5 = int(random.randint(0, 8))
                rtp_lapses6 = int(random.randint(0, 8))
                rtp_lapses7 = int(random.randint(0, 8))
                rtp_lapses8 = int(random.randint(0, 8))
                rtp_lapses9 = int(random.randint(0, 8))
                rtp_lapses10 = int(random.randint(0, 8))

                total_segments = 1000
                pkt_loss_seconds = int(random.randint(0, 15))
                rtp_call_seconds = int(random.randint(0, 15))
                rtp_calls_analyzed = int(random.randint(0, 15))
                rtp_calls_detected = int(random.randint(0, 15))


                fmt_syslog = timestamp + ' ' + hostname + ' gtp_ver=' + gtp_ver
                fmt_syslog += ' loc_type=' + loc_type + ' cell_id=' + cols[0]
                fmt_syslog += ' ul_data_vol=' + str(ul_data_vol) + ' dl_data_vol=' + str(dl_data_vol)
                fmt_syslog += ' ul_volte_vol=' + str(ul_volte_vol) + ' dl_volte_vol=' + str(dl_volte_vol)
                fmt_syslog += ' rtp_lapses1=' + str(rtp_lapses1)
                fmt_syslog += ' rtp_lapses2=' + str(rtp_lapses2)
                fmt_syslog += ' rtp_lapses3=' + str(rtp_lapses3)
                fmt_syslog += ' rtp_lapses4=' + str(rtp_lapses4)
                fmt_syslog += ' rtp_lapses5=' + str(rtp_lapses5)
                fmt_syslog += ' rtp_lapses6=' + str(rtp_lapses6)
                fmt_syslog += ' rtp_lapses7=' + str(rtp_lapses7)
                fmt_syslog += ' rtp_lapses8=' + str(rtp_lapses8)
                fmt_syslog += ' rtp_lapses9=' + str(rtp_lapses9)
                fmt_syslog += ' rtp_lapses10=' + str(rtp_lapses10)
                fmt_syslog += ' total_segments=' + str(total_segments)
                fmt_syslog += ' pkt_loss_seconds=' + str(pkt_loss_seconds)
                fmt_syslog += ' rtp_call_seconds=' + str(rtp_call_seconds)
                fmt_syslog += ' rtp_calls_analyzed=' + str(rtp_calls_analyzed)
                fmt_syslog += ' rtp_calls_detected=' + str(rtp_calls_detected)

                try:
                    if send:
                        sock.send(fmt_syslog)
                except socket.error as msg:
                    print (timestamp), msg
                    send = False
                    sock.close()
                    sock = None
                    break

            if sock is not None:
                sock.close()

    """
    Sleep for 30 seconds and generate syslog again
    """
    time.sleep(30)


Last edited by joeyg; 08-14-2015 at 11:11 AM.. Reason: Better title
# 2  
Old 08-13-2015
Please show us the diff output comparing your code now and your code when it was last working correctly.

What syslog messages are being logged?
# 3  
Old 08-14-2015
There is no output now. If it were an output format or content problem, I could fix it, but no such luck.
# 4  
Old 08-14-2015
You said:
Quote:
I added some fields to some existing code, and all was well. Just added a few more, and BAM.
What did you change between "all was well" and "Just added a few more, and BAM."? Show us the changes!

You said:
Quote:
The code generates syslog messages, and the new fields were added around "rtp_lapse*" . Any clues???
Show us the syslog messages the code generates!
# 5  
Old 08-17-2015
I don't see the code I posted with changes yesterday for some reason. However, I did find the problem. I copied the tested code from OS X onto our Redhat server, and on the new machine, invisible carriage returns were read as indentations.

Thanks all!

Last edited by gmark99; 08-17-2015 at 02:51 PM.. Reason: Salutory
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

4. Shell Programming and Scripting

Python script called by a shell script

experts, i wrote a python script to do a certain job, i tried it and it is working fine, i want this script to be executed automatically after a ksh script, the problem is when i execute the ksh script my python script runes perfectly after the ksh script as I have include it at the end of the ksh... (1 Reply)
Discussion started by: q8devilish
1 Replies

5. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

6. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

7. Programming

Python Script with C++Qt

Hi i work under CentOS 5.5, Qt 4.7, Python 2.4. i need to send data from C++ to Python. i write a code but it's not work, i have an Inferior System error on it. PyObject* objArg = Py_BuildValue("(z)", cKeyNum); PyObject* objFunc = (PyObject*)Init; pValue = PyEval_CallObject(objFunc, objArg);... (12 Replies)
Discussion started by: HanyM.Magdy
12 Replies

8. Shell Programming and Scripting

Hi Python and shell script,the script hangs

Hi I need to run a shell script from a TCL script,the shell script in trun will run a python script 1.Tcl script set filename "./GopiRun.sh" 2.GopiRun.sh python ./psi.py $MYSB/test_scripts/delivery/gpy1.py 3.I have my gpy1.py script. Here the problem i am facing is on running... (0 Replies)
Discussion started by: nathgopi214
0 Replies

9. Shell Programming and Scripting

How to run python script from a Tcl script

Hi I have a python script,i need to run this script from a tcl script.Can anyone let me know how to do this (1 Reply)
Discussion started by: nathgopi214
1 Replies
Login or Register to Ask a Question