I am trying to split a user:pass:host:port proxy into user:pass and host:port, I know how to get the user:pass using proxytest = proxy.split("@")[0] when the proxy is user:pass@localhost:8080, it returns user:pass but how can I get the localhost:8080? Preferably a very easy way if possible. The proxy is opened in a .txt file and may be changed so I do not know the exact string.
Asked
Active
Viewed 131 times
-6
-
1just use "user:pass@localhost:8080".split("@")[1] or localPath =proxy.split("@")[1] – toheedNiaz Apr 17 '18 at 17:32
-
4It seems unlikely that you're unable to modify the snippet that you currently have to access the second part. If you don't understand that line well enough to do that, I'd recommend a structured tutorial e.g. https://docs.python.org/3/tutorial/. – jonrsharpe Apr 17 '18 at 17:33
-
@toheedNiaz That's splitting a string not what I want – Just A Coder Apr 17 '18 at 17:34
-
@jonrsharpe It seems stupid that something so simple like that would be impossible? – Just A Coder Apr 17 '18 at 17:34
-
What do you mean that's not what you want? It's what you're doing now! Of course this isn't impossible, indeed it's so trivial that the only charitable assumption is that you haven't correctly explained the problem. I'd recommend you [edit] the question. – jonrsharpe Apr 17 '18 at 17:37
-
@jonrsharpe I dont see whats wrong with the question – Just A Coder Apr 17 '18 at 17:38
-
I just need to get localhost:8080 when I split user:pass and local:host via the @ – Just A Coder Apr 17 '18 at 17:39
-
user:pass and localhost:8080 are just examples of what the proxy could be – Just A Coder Apr 17 '18 at 17:39
-
Check out urlparse for [Python 2](https://docs.python.org/2/library/urlparse.html) or [Python 3](https://docs.python.org/3/library/urllib.parse.html) – Sunny Patel Apr 17 '18 at 17:40
-
The answer was really simple my friend helped me prox = "user:pass@host:port" userpass = prox[0:prox.find("@")] hostport = prox[prox.find("@")+1:] – Just A Coder Apr 17 '18 at 17:44
-
That's doing what the first comment suggested, which you explicitly said you didn't want, but in a less readable way. – jonrsharpe Apr 17 '18 at 17:47
2 Answers
0
In Python 2 you can use the urlparse.urlsplit
function to accomplish this without any actual parsing.
from urlparse import *
x = "http://user:pass@localhost:8080"
parts = urlsplit(x)
print parts.username #Prints 'user'
print parts.password #Prints 'pass'
print parts.hostname #Prints 'localhost'
print parts.port #Prints '8080'
If you were looking for something really naive, you can split on the @
.
x = "user:pass@localhost:8080"
userpass, hostport = x.split('@')
print userpass, hostport #Prints 'user:pass localhost:8080'

Sunny Patel
- 7,830
- 2
- 31
- 46
-1
The answer was really simple my friend helped me
prox = "user:pass@host:port"
userpass = prox[0:prox.find("@")]
hostport = prox[prox.find("@")+1:]

tripleee
- 175,061
- 34
- 275
- 318

Just A Coder
- 1
- 4