murali koripally
murali koripally

Reputation: 113

Replace port in url using python

I want to change the port in given url.

OLD=http://test:7000/vcc3 NEW=http://test:7777/vcc3

I tried below code code, I am able to change the URL but not able to change the port.

>>> from urlparse import urlparse
>>> aaa = urlparse('http://test:7000/vcc3')
>>> aaa.hostname
test
>>> aaa.port
7000
>>>aaa._replace(netloc=aaa.netloc.replace(aaa.hostname,"newurl")).geturl()
'http://newurl:7000/vcc3'
>>>aaa._replace(netloc=aaa.netloc.replace(aaa.port,"7777")).geturl()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object

Upvotes: 9

Views: 7842

Answers (3)

bukas
bukas

Reputation: 181

The problem is that the ParseResult 's 'port' member is protected and you can't change the attribute -don't event try to use private _replace() method. Solution is here:

from urllib.parse import urlparse, ParseResult

old = urlparse('http://test:7000/vcc3')
new = ParseResult(scheme=a.scheme, netloc="{}:{}".format(old.hostname, 7777),
                  path=old.path, params=old.params, query=old.query, fragment=old.fragment)
new_url = new.geturl()

The second idea is to convert ParseResult to list->change it later on like here:

Changing hostname in a url

BTW 'urlparse' library is not flexible in that area!

Upvotes: 3

AChampion
AChampion

Reputation: 30258

The details of the port are stored in netloc, so you can simply do:

>>> a = urlparse('http://test:7000/vcc3')
>>> a._replace(netloc='newurl:7777').geturl()
'http://newurl:7777/vcc3'
>>> a._replace(netloc=a.hostname+':7777').geturl()  # Keep the same host
'http://test:7777/vcc3'

Upvotes: 8

Benjamin Hodgson
Benjamin Hodgson

Reputation: 44634

It's not a particularly good error message. It's complaining because you're passing ParseResult.port, an int, to the string's replace method which expects a str. Just stringify port before you pass it in:

aaa._replace(netloc=aaa.netloc.replace(str(aaa.port), "7777"))

I'm astonished that there isn't a simple way to set the port using the urlparse library. It feels like an oversight. Ideally you'd be able to say something like parseresult._replace(port=7777), but alas, that doesn't work.

Upvotes: 11

Related Questions