Replace port in url using python -


i want change port in given url.

old=http://test:7000/vcc3 new=http://test:7777/vcc3

i tried below code code, able change url not able change port.

>>> 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 character buffer object 

it's not particularly error message. it's complaining because you're passing parseresult.port, int, string's replace method expects str. stringify port before pass in:

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

i'm astonished there isn't simple way set port using urlparse library. feels oversight. ideally you'd able parseresult._replace(port=7777), alas, that doesn't work.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -