简体   繁体   English

无法生成执行文件的正确路径-python

[英]Cant generate the correct path to execute a file - python

I am trying to run ventrilo from python and join a channel, 我正在尝试从python运行ventrilo并加入一个频道,

    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. 我在ventrilo中得到的实际路径是: CS.vGames.co.il GatherBot ,因为缺少频道-2-/.Team-A

I know that it can be done because when I do it from my IRC client it works. 我知道可以做到这一点,因为当我从IRC客户端执行此操作时,它可以工作。 In my IRC clinet I use %20 for spaces for that to work. 在我的IRC客户端中,我使用%20来使它正常工作。

In python I tried everything I could find on the internet. 在python中,我尝试了所有可以在互联网上找到的内容。 Please help me. 请帮我。

The right answer here is to build the query string part of the URL with urllib.urlencode . 正确的答案是使用urllib.urlencode构建URL的查询字符串部分。 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: 如果您有其他地方的硬编码查询字符串,则可以在其上使用urllib.quote

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.). 或者,如果你从别的地方得到了一个完整的硬编码网址,该网址应该已经正确编码(用“%20”为“”,等等)。 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. 如果您手动编写URL,则始终可以通过手工编码,只需将%20替换为空格,然后编码其他所有您注意到的内容,直到它起作用为止……但这通常不是一种有效的方法,只能用于快速,肮脏的黑客攻击。在翻译中。

You can try something like: use urlparse.urlparse on it, then encode or quote the query string part and put it back together. 你可以尝试这样的:使用urlparse.urlparse它,然后encodequote查询字符串的一部分,并把它重新走到一起。 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. 例如,频道名称中有一个未编码“/”,因此谁知道urlparse将做到这一点。 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; 但是最好的解决方案是退后一步:无论URL来自何处,某人在某个时候都必须知道什么是路径,什么是查询字符串以及如何联接查询参数等等。 capture the knowledge at that point and pass it down, and do things the right way with urllib.urlencode . 捕获当时的知识并将其传递下来,并使用urllib.urlencode以正确的方式进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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