English 中文(简体)
你们如何用包含形式特征的URL来扼杀干预?
原标题:How do you do string interpolation with a URL that contains formatting characters?
  • 时间:2010-10-28 16:02:27
  •  标签:
  • python

I m试图利用URLIB2打开URL,并将内容重新插入一个阵列。 问题似乎在于,你不能使用聚氯乙烯进行扼杀,这种电离层特性包括:对空间的20%;对地的%3C; 。 危房的面积和面积为xml。

My code is pretty simple, looks something like this:

#Python script to fetch NS Client Policies using GUID

import sys
import urllib2

def GetPolicies(ns, guid):
    ns = sys.argv[1]
    guid = sys.argv[2]
    fetch = urllib2.urlopen( http://%s/Altiris/NS/Agent/GetClientPolicies.aspx?xml=%3Crequest%20configVersion=%222%22%20guid=%22{%s}%22 ) % (ns, guid)

我已经缩短了卢爱阵的简短性,但大家一般认为,由于你假设你再次想要使用 %3、200和其他东西作为扼杀性争辩,你就没有就显示错误的格式提出充分的理由。 你们如何这样做?

Edit: Solution requires Python 2.6+, 2.5 or prior has no support for the string.format() method

最佳回答

页: 1

url =  http://%s/Altiris/NS/Agent/GetClientPolicies.aspx?xml=%%3Crequest%%20configVersion=%%222%%22%%20guid=%%22{%s}%%22  % (ns, guid)

或可使用<代码>。

url =  http://{hostname}/Altiris/NS/Agent/GetClientPolicies.aspx?xml=%3Crequest%20configVersion=%222%22%20guid=%22{id}%%2  .format(hostname=ns, id=guid)
问题回答

使用<代码>.format 方法替代插图。 文件:

str.format(*args, **kwargs) 
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

>>> "The sum of 1 + 2 is {0}".format(1+2)
 The sum of 1 + 2 is 3 

当我们从C改用时,我们全都坚持%<>/code>,而format方法实际上是将价值观推向扼杀的一种更强有力的方法。

在步骤中建设你的力量,使每个层次都分开。 远比试图应对紧急情况的多层次因素,更能够管理。

xml=  <request configVersion="2" guid="{%s}"/>  % cgi.escape(guid, True)
query=  xml=%s  % urllib2.quote(xml)
url=  http://%s/Altiris/NS/Agent/GetClientPolicies.aspx?%s  % (ns, query)
fetch= urllib2.urlopen(url)

If you re trying to build the url yourself use urllib.urlencode. It will deal with a lot of the quoting issues for you. Just pass it a dict of the info you want:

from urllib import urlencode

args = urlencode({ xml :  < ,
            request configVersion :  bar ,
                guid :  zomg })

至于替换你在座右铭的座标,就象其他人所说的话一样,也使用百分比的格式。 最终扼杀可能像:

print  http://%s/Altiris/NS/Agent/GetClientPolicies.aspx?%s  % ( foobar.com , args)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...