replacing \n characters in a hash

J

Johhny

Hello,

I am currently trying to write some scripts to get information from the
xmlrpc for redhat network. One of the issues I am having is trying to
strip off the special characters in the hash that is returned. Here is
an example of the information returned within the hash :

===SNIP===
{'errata_update_date': '2005-12-06', 'errata_topic': 'Updated
libc-client packages that fix a buffer overflow issue are
now\navailable.\n\nThis update has been rated as having moderate
security impact by the Red\nHat Security Response Team.',
'errata_type': 'Security Advisory', 'errata_notes': '',
'errata_synopsis': 'Moderate: libc-client security update',
'errata_references': '', 'errata_last_modified_date': '2006-01-25
10:37:24', 'errata_issue_date': '2005-12-06', 'errata_description':
'C-client is a common API for accessing mailboxes.\n\nA buffer overflow
flaw was discovered in the way C-client parses user\nsupplied
mailboxes. If an authenticated user requests a specially
crafted\nmailbox name, it may be possible to execute arbitrary code on
a server that\nuses C-client to access mailboxes. The Common
Vulnerabilities and Exposures\nproject has assigned the name
CVE-2005-2933 to this issue.\n\nAll users of libc-client should upgrade
to these updated packages, which\ncontain a backported patch that
resolves this issue.'}
===SNIP===


What I would like to do is remove the \n characters from
'errata_topic'. Which is this section of the hash.

Updated libc-client packages that fix a buffer overflow issue are
now\navailable.\n\nThis update has been rated as having moderate
security impact by the Red\nHat Security Response Team.

What I had attempted to do is use the replace() function but it
consistantly comes up with the following errors:

Traceback (most recent call last):
File "rhn_errata.py", line 63, in ?
errata_package = errata_package.strip('\n','')
AttributeError: 'dict' object has no attribute 'strip'

where errata_package is JUST the errata_topic hash value.

Any advice would be great on how to do that.

Regards,

Johhny
 
C

Carsten Haese

Hello,

I am currently trying to write some scripts to get information from the
xmlrpc for redhat network. One of the issues I am having is trying to
strip off the special characters in the hash that is returned. Here is
an example of the information returned within the hash :

===SNIP===
{'errata_update_date': '2005-12-06', 'errata_topic': 'Updated
libc-client packages that fix a buffer overflow issue are
now\navailable.\n\nThis update has been rated as having moderate
security impact by the Red\nHat Security Response Team.',

[...]

What I had attempted to do is use the replace() function but it
consistantly comes up with the following errors:

Traceback (most recent call last):
File "rhn_errata.py", line 63, in ?
errata_package = errata_package.strip('\n','')
AttributeError: 'dict' object has no attribute 'strip'

where errata_package is JUST the errata_topic hash value.

errata_package is obviously not just the errata_topic hash value,
because that would be a string, and python for some reason seems to
believe that errata_package is a dictionary.

Also note that once you correct that problem, python will complain that
strip() doesn't take 2 parameters, since you actually mean replace().

-Carsten
 
J

Johhny

Hello,

Thankyou for your response,
If I check that the errara_package value is with a print I get the
following.

===SNIP===
Updated libc-client packages that fix a buffer overflow issue are now
available.

This update has been rated as having moderate security impact by the
Red
Hat Security Response Team.
===SNIP===

Notice that it has formatted the output with the \n's.

So i dont understand why its reporting as a dictionary rather than just
the string.
 
C

Carsten Haese

Hello,

Thankyou for your response,
If I check that the errara_package value is with a print I get the
following.

===SNIP===
Updated libc-client packages that fix a buffer overflow issue are now
available.

This update has been rated as having moderate security impact by the
Red
Hat Security Response Team.
===SNIP===

Notice that it has formatted the output with the \n's.

So i dont understand why its reporting as a dictionary rather than just
the string.

Posting relevant bits of your code might help.

-Carsten
 
J

Johhny

Hello,

Here is the code (minus my details section).

server = xmlrpclib.ServerProxy(url)

session = server.auth.login(username,password)

#functions.

def getErrata():

channel_label = 'rhel-i386-as-4'

errata =
server.channel.software.list_errata(session,channel_label,start_date,end_date)

return errata



def getPackage(advisory):

Package = server.errata.get_details(session,advisory)

return Package



errata = getErrata()

for vals in errata:

print "%s\t\t%s\t\t%s\t%s\t%s" %
(vals['errata_advisory'],vals['errata_issue_date'],vals['errata_update_date'],vals['errata_last_modified_date'],vals['errata_type'],)

errata_info = getPackage(vals['errata_advisory'],)

print errata_info['errata_topic']

errata_package = errata_info['errata_topic']

print getPackage(vals['errata_advisory'])

I have not got any of the section in to replace the \n's as I was
trying to work out why its not seeing what I thought was a string as a
dict.
 
L

Larry Bates

Johhny said:
Hello,

I am currently trying to write some scripts to get information from the
xmlrpc for redhat network. One of the issues I am having is trying to
strip off the special characters in the hash that is returned. Here is
an example of the information returned within the hash :

===SNIP===
{'errata_update_date': '2005-12-06', 'errata_topic': 'Updated
libc-client packages that fix a buffer overflow issue are
now\navailable.\n\nThis update has been rated as having moderate
security impact by the Red\nHat Security Response Team.',
'errata_type': 'Security Advisory', 'errata_notes': '',
'errata_synopsis': 'Moderate: libc-client security update',
'errata_references': '', 'errata_last_modified_date': '2006-01-25
10:37:24', 'errata_issue_date': '2005-12-06', 'errata_description':
'C-client is a common API for accessing mailboxes.\n\nA buffer overflow
flaw was discovered in the way C-client parses user\nsupplied
mailboxes. If an authenticated user requests a specially
crafted\nmailbox name, it may be possible to execute arbitrary code on
a server that\nuses C-client to access mailboxes. The Common
Vulnerabilities and Exposures\nproject has assigned the name
CVE-2005-2933 to this issue.\n\nAll users of libc-client should upgrade
to these updated packages, which\ncontain a backported patch that
resolves this issue.'}
===SNIP===


What I would like to do is remove the \n characters from
'errata_topic'. Which is this section of the hash.

Updated libc-client packages that fix a buffer overflow issue are
now\navailable.\n\nThis update has been rated as having moderate
security impact by the Red\nHat Security Response Team.

What I had attempted to do is use the replace() function but it
consistantly comes up with the following errors:

Traceback (most recent call last):
File "rhn_errata.py", line 63, in ?
errata_package = errata_package.strip('\n','')
AttributeError: 'dict' object has no attribute 'strip'

where errata_package is JUST the errata_topic hash value.

Any advice would be great on how to do that.

Regards,

Johhny
You must use 'errata_topic' as index into your dictionary
to get the string and then replace the \n chars.

Example assumes that the dict is pointed to by d):

errata_topic_text=d['errata_topic'].replace('\n',' ')

Hope this helps.

Larry Bates
 
F

Fredrik Lundh

Johhny wrote:

for vals in errata:

print "%s\t\t%s\t\t%s\t%s\t%s" %
(vals['errata_advisory'],vals['errata_issue_date'],vals['errata_update_date'],vals['errata_last_modified_date'],vals['errata_type'],
)

errata_info = getPackage(vals['errata_advisory'],)

print errata_info['errata_topic']

errata_package = errata_info['errata_topic']

add

print type(errata_package), repr(errata_package)

here, and let us know what it prints.
print getPackage(vals['errata_advisory'])

</F>
 
S

Steve Holden

Johhny said:
Hello,

Here is the code (minus my details section).

server = xmlrpclib.ServerProxy(url)

session = server.auth.login(username,password)

#functions.

def getErrata():

channel_label = 'rhel-i386-as-4'

errata =
server.channel.software.list_errata(session,channel_label,start_date,end_date)

return errata



def getPackage(advisory):

Package = server.errata.get_details(session,advisory)

return Package



errata = getErrata()

for vals in errata:

print "%s\t\t%s\t\t%s\t%s\t%s" %
(vals['errata_advisory'],vals['errata_issue_date'],vals['errata_update_date'],vals['errata_last_modified_date'],vals['errata_type'],)

errata_info = getPackage(vals['errata_advisory'],)

print errata_info['errata_topic']

errata_package = errata_info['errata_topic']

errata_package = errata_info['errata_topic'].replace('\n', " ")
print getPackage(vals['errata_advisory'])

I have not got any of the section in to replace the \n's as I was
trying to work out why its not seeing what I thought was a string as a
dict.

regards
Steve
 
J

Johhny

Hello,

In response to that the output is this :

<type 'str'> 'Updated libc-client packages that fix a buffer overflow
issue are now\navailable.\n\nThis update has been rated as having
moderate security impact by the Red\nHat Security Response Team.'
 
J

Johhny

Hello All,

thanks for your help. I got it working and learnt some more things
which is always great. Your assitance has been very useful.

Regards,

Johhny
 
F

Fredrik Lundh

(since this is a newsgroup, can you please quote the message
you're replying to).
In response to that the output is this :

<type 'str'> 'Updated libc-client packages that fix a buffer overflow
issue are now\navailable.\n\nThis update has been rated as having
moderate security impact by the Red\nHat Security Response Team.'

<type 'str> means that it *is* a string, after all. looks like you didn't
post the code you were using :-(

replacing the print statement with

errata_package = errata_package.replace("\n", " ")

should do what you want.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top