I'm trying to create a program that can analyze the speed of a website in the terminal. I have used the curl module to process the command in Linux shell. But I'm getting the following error.
ValueError: unsupported format character '{' (0x7b) at index 40
import subprocess
def webSpeed():
website = raw_input("Enter name of the website: ")
print(website)
cmd = "curl -s -w 'Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nAppCon Time:\t\t%{time_appconnect}\nRedirect Time:\t\t%{time_redirect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://%s" %(webiste)
print(cmd)
temp = subprocess.call(cmd, shell="TRUE")
print(temp)
How can I add curl commands in python if it's using curly braces?
When you do
string % format_values
Then every %
in string is treated as formatting place. Python found %{
and was confused - it expected %d
for placing integers or %s
for placing strings, or something else know to it. But formatting code does not use %{
for any format - thus error. When you want to write %
which does not have formatting meaning then you should escape it by another %
.
Therefore there are two solutions
%s
) with %%
string % format_values
but simply add website to your command since luckily it is at the endCorrect and wrong code examples:
website = "lukaszslusarczyk.pl"
cmd1 = "curl -s -w 'Response Time for %%{url_effective}\t%%{time_connect}\n' -o /dev/null https://%s" % website # OK
cmd2 = "curl -s -w 'Response Time for %{url_effective}\t%{time_connect}\n' -o /dev/null https://" + website # OK
cmd3 = "curl -s -w 'Response Time for %{url_effective}\t%{time_connect}\n' -o /dev/null https://%s" % website # wrong
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.