get UTC time directly from a website in python











up vote
0
down vote

favorite
1












i want to get the utc time directly from a website that can give that. and i don't want to use python modules because they give me the system time and my system time does not show the correct time.
how can i get the correct utc time without changing the system time?
i use



import ntplib,datetime
x = ntplib.NTPClient()
datetime.datetime.utcfromtimestamp(x.request('europe.pool.ntp.org').tx_time)


but i get this error



raise NTPException("No response received from %s." % host)
ntplib.NTPException: No response received from europe.pool.ntp.org.









share|improve this question




















  • 1




    are you behind a proxy? it works for me.
    – Sufiyan Ghori
    16 hours ago










  • @Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
    – ali
    16 hours ago










  • FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
    – deceze
    16 hours ago












  • From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
    – vikas singhal
    16 hours ago















up vote
0
down vote

favorite
1












i want to get the utc time directly from a website that can give that. and i don't want to use python modules because they give me the system time and my system time does not show the correct time.
how can i get the correct utc time without changing the system time?
i use



import ntplib,datetime
x = ntplib.NTPClient()
datetime.datetime.utcfromtimestamp(x.request('europe.pool.ntp.org').tx_time)


but i get this error



raise NTPException("No response received from %s." % host)
ntplib.NTPException: No response received from europe.pool.ntp.org.









share|improve this question




















  • 1




    are you behind a proxy? it works for me.
    – Sufiyan Ghori
    16 hours ago










  • @Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
    – ali
    16 hours ago










  • FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
    – deceze
    16 hours ago












  • From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
    – vikas singhal
    16 hours ago













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





i want to get the utc time directly from a website that can give that. and i don't want to use python modules because they give me the system time and my system time does not show the correct time.
how can i get the correct utc time without changing the system time?
i use



import ntplib,datetime
x = ntplib.NTPClient()
datetime.datetime.utcfromtimestamp(x.request('europe.pool.ntp.org').tx_time)


but i get this error



raise NTPException("No response received from %s." % host)
ntplib.NTPException: No response received from europe.pool.ntp.org.









share|improve this question















i want to get the utc time directly from a website that can give that. and i don't want to use python modules because they give me the system time and my system time does not show the correct time.
how can i get the correct utc time without changing the system time?
i use



import ntplib,datetime
x = ntplib.NTPClient()
datetime.datetime.utcfromtimestamp(x.request('europe.pool.ntp.org').tx_time)


but i get this error



raise NTPException("No response received from %s." % host)
ntplib.NTPException: No response received from europe.pool.ntp.org.






python utc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago









Sufiyan Ghori

10.7k95580




10.7k95580










asked 16 hours ago









ali

32




32








  • 1




    are you behind a proxy? it works for me.
    – Sufiyan Ghori
    16 hours ago










  • @Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
    – ali
    16 hours ago










  • FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
    – deceze
    16 hours ago












  • From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
    – vikas singhal
    16 hours ago














  • 1




    are you behind a proxy? it works for me.
    – Sufiyan Ghori
    16 hours ago










  • @Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
    – ali
    16 hours ago










  • FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
    – deceze
    16 hours ago












  • From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
    – vikas singhal
    16 hours ago








1




1




are you behind a proxy? it works for me.
– Sufiyan Ghori
16 hours ago




are you behind a proxy? it works for me.
– Sufiyan Ghori
16 hours ago












@Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
– ali
16 hours ago




@Sufiyan Ghori yes. is there another website that i can replace in this code?or any other way for this question?
– ali
16 hours ago












FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
– deceze
16 hours ago






FWIW, this should really be fixed by installing an NTP client as a constant background service on the machine to keep the actual machine time accurate, if at all possible.
– deceze
16 hours ago














From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
– vikas singhal
16 hours ago




From the response looks like site is not reachable from your computer. I tried the same code from my local desktop and it worked fine.
– vikas singhal
16 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Could you try to call datetime.utcnow()? From the python docs



Here is my output



>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 11, 4, 9, 47, 59, 572104)


Edit as OP having issues with above



As for a web based solution, this works fine for me (Whilst avoiding your website



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> print(x.content)
b'abbreviation: GMTndatetime: 2018-11-04T10:09:18.395273+00:00nday_of_week: 0nday_of_year: 308ndst: falsendst_from: ndst_until: ntimezone: Europe/Londonnunixtime: 1541326158nutc_offset: +00:00nweek_number: 44'


Update



You can get the datetime like so. I am certain there is a better way to do this, but this method works fine for this specific use-case.



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> y = x.text
>>> print(y[27:47])
2018-11-04T11:01:46





share|improve this answer










New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
    – ali
    16 hours ago












  • It should give you utc time. I will update my answer with a web-based solution also.
    – BGlasbey
    16 hours ago










  • thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
    – ali
    15 hours ago










  • Updated for you.
    – BGlasbey
    15 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139354%2fget-utc-time-directly-from-a-website-in-python%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Could you try to call datetime.utcnow()? From the python docs



Here is my output



>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 11, 4, 9, 47, 59, 572104)


Edit as OP having issues with above



As for a web based solution, this works fine for me (Whilst avoiding your website



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> print(x.content)
b'abbreviation: GMTndatetime: 2018-11-04T10:09:18.395273+00:00nday_of_week: 0nday_of_year: 308ndst: falsendst_from: ndst_until: ntimezone: Europe/Londonnunixtime: 1541326158nutc_offset: +00:00nweek_number: 44'


Update



You can get the datetime like so. I am certain there is a better way to do this, but this method works fine for this specific use-case.



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> y = x.text
>>> print(y[27:47])
2018-11-04T11:01:46





share|improve this answer










New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
    – ali
    16 hours ago












  • It should give you utc time. I will update my answer with a web-based solution also.
    – BGlasbey
    16 hours ago










  • thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
    – ali
    15 hours ago










  • Updated for you.
    – BGlasbey
    15 hours ago















up vote
0
down vote



accepted










Could you try to call datetime.utcnow()? From the python docs



Here is my output



>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 11, 4, 9, 47, 59, 572104)


Edit as OP having issues with above



As for a web based solution, this works fine for me (Whilst avoiding your website



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> print(x.content)
b'abbreviation: GMTndatetime: 2018-11-04T10:09:18.395273+00:00nday_of_week: 0nday_of_year: 308ndst: falsendst_from: ndst_until: ntimezone: Europe/Londonnunixtime: 1541326158nutc_offset: +00:00nweek_number: 44'


Update



You can get the datetime like so. I am certain there is a better way to do this, but this method works fine for this specific use-case.



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> y = x.text
>>> print(y[27:47])
2018-11-04T11:01:46





share|improve this answer










New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
    – ali
    16 hours ago












  • It should give you utc time. I will update my answer with a web-based solution also.
    – BGlasbey
    16 hours ago










  • thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
    – ali
    15 hours ago










  • Updated for you.
    – BGlasbey
    15 hours ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Could you try to call datetime.utcnow()? From the python docs



Here is my output



>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 11, 4, 9, 47, 59, 572104)


Edit as OP having issues with above



As for a web based solution, this works fine for me (Whilst avoiding your website



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> print(x.content)
b'abbreviation: GMTndatetime: 2018-11-04T10:09:18.395273+00:00nday_of_week: 0nday_of_year: 308ndst: falsendst_from: ndst_until: ntimezone: Europe/Londonnunixtime: 1541326158nutc_offset: +00:00nweek_number: 44'


Update



You can get the datetime like so. I am certain there is a better way to do this, but this method works fine for this specific use-case.



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> y = x.text
>>> print(y[27:47])
2018-11-04T11:01:46





share|improve this answer










New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









Could you try to call datetime.utcnow()? From the python docs



Here is my output



>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 11, 4, 9, 47, 59, 572104)


Edit as OP having issues with above



As for a web based solution, this works fine for me (Whilst avoiding your website



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> print(x.content)
b'abbreviation: GMTndatetime: 2018-11-04T10:09:18.395273+00:00nday_of_week: 0nday_of_year: 308ndst: falsendst_from: ndst_until: ntimezone: Europe/Londonnunixtime: 1541326158nutc_offset: +00:00nweek_number: 44'


Update



You can get the datetime like so. I am certain there is a better way to do this, but this method works fine for this specific use-case.



>>> import requests
>>> x = requests.get('http://worldtimeapi.org/api/timezone/Europe/London.txt')
>>> y = x.text
>>> print(y[27:47])
2018-11-04T11:01:46






share|improve this answer










New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited 15 hours ago





















New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 16 hours ago









BGlasbey

385




385




New contributor




BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






BGlasbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
    – ali
    16 hours ago












  • It should give you utc time. I will update my answer with a web-based solution also.
    – BGlasbey
    16 hours ago










  • thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
    – ali
    15 hours ago










  • Updated for you.
    – BGlasbey
    15 hours ago


















  • i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
    – ali
    16 hours ago












  • It should give you utc time. I will update my answer with a web-based solution also.
    – BGlasbey
    16 hours ago










  • thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
    – ali
    15 hours ago










  • Updated for you.
    – BGlasbey
    15 hours ago
















i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
– ali
16 hours ago






i try that and it gives me the system time which i use. and my system doesn't show the correct time.i want to receive correct time although my system doesn't set on the correct time.
– ali
16 hours ago














It should give you utc time. I will update my answer with a web-based solution also.
– BGlasbey
16 hours ago




It should give you utc time. I will update my answer with a web-based solution also.
– BGlasbey
16 hours ago












thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
– ali
15 hours ago




thank you it works. could you tell me how can i separate the unix time and put it in a separate variable?
– ali
15 hours ago












Updated for you.
– BGlasbey
15 hours ago




Updated for you.
– BGlasbey
15 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139354%2fget-utc-time-directly-from-a-website-in-python%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud