Visit The New, Modern Unix Linux Community

Help with syntax of Python script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with syntax of Python script
# 1  
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???

__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

# 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_id_count += 1

# 20 percentage syslog
syslog_count = global_cell_id_count * 20 / 100

UDP_IP = ""
UDP_PORT = 10516

gtp_ver = '2'
loc_type = '4'

hostname = socket.gethostname()
sock = None

# Infinite Loop
while not stop:

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

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

        sock.connect((UDP_IP, UDP_PORT))

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


        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)


                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)

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

            if sock is not None:

    Sleep for 30 seconds and generate syslog again

Last edited by joeyg; 08-14-2015 at 11:11 AM.. Reason: Better title
# 2  
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  
There is no output now. If it were an output format or content problem, I could fix it, but no such luck.
# 4  
You said:
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:
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  
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

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #738
Difficulty: Medium
RadioShack introduced a 50 MB external hard disk for the TRS-80 Model III/4 in 1983.
True or False?

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 which inturn call the python script, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: a=3 b=3; c= a+b exec python 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 "./" python ./ $MYSB/test_scripts/delivery/ 3.I have my 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

Featured Tech Videos