简体   繁体   中英

Cant generate the correct path to execute a file - python

I am trying to run ventrilo from python and join a channel,

    path= 'ventrilo://91.227.221.73:3824/servername=vGames&serverpassword=9929&channelname=CS.vGames.co.il GatherBot -2-/.Team-A&channelpassword=57'

    os.startfile(path)

The actual path I get in the ventrilo is: CS.vGames.co.il GatherBot , for the channel -2-/.Team-A is missing.

I know that it can be done because when I do it from my IRC client it works. In my IRC clinet I use %20 for spaces for that to work.

In python I tried everything I could find on the internet. Please help me.

The right answer here is to build the query string part of the URL with urllib.urlencode . For example:

qparams = {
  'servername': 'vGames', 
  'serverpassword': '9929', 
  'channelname': 'CS.vGames.co.il GatherBot -2-/.Team-',
  'channelpassword': '57' }
path = 'ventrilo://91.227.221.73:3824/' + urllib.urlencode(qparams)

If you've got a hardcoded query string from somewhere else, you can just use urllib.quote on it:

qstring = 'servername=vGames&serverpassword=9929&channelname=CS.vGames.co.il GatherBot -2-/.Team-A&channelpassword=57'
path = 'ventrilo://91.227.221.73:3824/' + urllib.quote(qstring)

Or, if you've got a complete hardcoded URL from somewhere else, that URL should already be properly encoded (with "%20" for " ", etc.). If you wrote the URL by hand, you can always encode it by hand, by just replacing spaces with %20, and encoding anything else you notice until it works… But this is generally not a good way to work for anything but really quick&dirty hacking around in the interpreter.

You can try something like: use urlparse.urlparse on it, then encode or quote the query string part and put it back together. But there's no guarantee it'll do the right thing; you're giving it garbage, and it doesn't necessarily know what's garbage about it. For example, the channel name has an unencoded '/' in it, so who knows what urlparse will do with that. Not to mention that you don't even have a '?' to introduce the query string.

So, you may need some custom code to work around the problem.

But the best solution is to take a step back: wherever the URL came from, someone at some point must have known what was path and what was query string and how to join query parameters and so on; capture the knowledge at that point and pass it down, and do things the right way with urllib.urlencode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM