import http.client
import time
import os
def get_webservertime(host):
conn=http.client.HTTPConnection(host)
conn.request("GET", "/")
r=conn.getresponse()
#r.getheaders() #获取所有的http头
ts= r.getheader('date') #获取http头date部分
print(ts) #将GMT时间转换成北京时间
ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
print(ltime)
ttime=time.localtime(time.mktime(ltime)+8*60*60)
print(ttime)
dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
print (dat,tm)
os.system(dat)
os.system(tm) get_webservertime('www.jd.com')

  

import urllib.request
import time def get_webservertime(url):
#返回一个对象
response=urllib.request.urlopen(url)
#打印出远程服务器返回的header信息
#print (response.info())
header=response.info() ts=header._headers[1][1] #将GMT时间转换成北京时间
ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
ttime=time.localtime(time.mktime(ltime)+8*60*60)
dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
print (dat,tm) get_webservertime('https://www.jd.com/')

  

import http.client
import time
def get_webservertime(host):
while True:
try:
conn=http.client.HTTPConnection(host)
conn.request("GET", "/")
r=conn.getresponse()
ts= r.getheader('date') #获取http头date部分
break
except Exception as e:
print(e)
continue
#将GMT时间转换成北京时间
ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
ttime=time.localtime(time.mktime(ltime)+8*60*60)
dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
timeStr=dat+' '+tm
return timeStr url='www.jd.com'
while True:
print(get_webservertime(url))

  

def get_webservertime():
url='https://ai.jd.com/jdip/useripinfo.php?callback=jsonpCallbackUserIpInfo'
while True:
try:
response=urllib.request.urlopen(url)
header=response.info()
break
except Exception as e:
print(e)
time.sleep(1)
continue #打印出远程服务器返回的header信息
ts=header._headers[1][1]
#将GMT时间转换成北京时间
ltime= time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
ttime=time.localtime(time.mktime(ltime)+8*60*60)
dat="%u-%02u-%02u"%(ttime.tm_year,ttime.tm_mon,ttime.tm_mday)
tm="%02u:%02u:%02u"%(ttime.tm_hour,ttime.tm_min,ttime.tm_sec)
timeStr=dat+' '+tm
return timeStr

  

【Python】获取服务器时间的更多相关文章

  1. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  2. JS实现Ajax---例:获取服务器时间

    Ajax在本质上是一个浏览器端的技术 XMLHttpRequest XMLHttpRequest对象 XMLHttpRequest对象在IE浏览器和非IE浏览器中创建的方法不同. 简而言之:它可以异步 ...

  3. php获取服务器时间的代码

    php获取服务器时间的代码. 用php的date函数即可来获取服务器上的时间:  <?php //将时区设置为中国 date_default_timezone_set("PRC&quo ...

  4. python获取网络时间和本地时间

    今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释. python获取网络时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. Unity 获取服务器时间 HTTP请求方式

    在写每日签到的时候,我居然使用的是本地时间...被项目经理笑哭了...., 如果你在写单机游戏,没有游戏服务器,但又不想使用本地时间,就可以采用下面方法. 方法总结: 1. 使用HTTP请求获取服务器 ...

  6. Javascript获取服务器时间

    //获取服务器时间 var getServerDate = function () {     var xmlHttpRequest = null,     serverDate = new Date ...

  7. python 获取当前时间(关于time()时间问题的重要补充)

    python 获取当前时间   我有的时候写程序要用到当前时间,我就想用python去取当前的时间,虽然不是很难,但是老是忘记,用一次丢一次,为了能够更好的记住,我今天特意写下python 当前时间这 ...

  8. Python获取服务器的厂商和型号信息-乾颐堂

    Python获取服务器的厂商和型号信息,在RHEHL6下,需要系统预装python-dmidecode这个包(貌似默认就已经装过了) 脚本内容如下 [root@linuxidc tmp]# cat t ...

  9. JS获取服务器时间并且计算距离当前指定时间差的函数

    项目中遇到了从服务器获取时间,现在记录一下方便以后查询: 1.后台代码:(创建一个date对象并以JSON的形式返回去) // 获取服务器时间 public String getNowServerTi ...

  10. JS 中获取服务器时间的注意点

    在通过js获取服务器时间时,遇到了小小的问题,但造成的影响挺大的,所以写出来提醒大家,在获取服务器时间时一定要细心要多测试多验证. js 中使用以下方法获取服务器时间时要注意两点: 1.xhr.ope ...

随机推荐

  1. C语言学习笔记 (007) - 数组指针和通过指针引用数组元素的方法总结

    1.数组指针:即指向数组的指针 那么, 如何声明一个数组指针呢? ]; /*括号是必须写的,不然就是指针数组:10是数组的大小*/ 拓展:有指针类型元素的数组称为指针数组. 2.通过指针引用数组元素的 ...

  2. php 执行命令函数

    /** Method to execute a command in the terminal Uses : 1. system 2. passthru 3. exec 4. shell_exec * ...

  3. 如何禁用Visual Studio 2013的浏览器链接功能

    VS2013新增的Browser Link功能虽然“强大”,但我并不需要. 但默认是开启的,会在页面中自动添加如下的代码,真是烦人! <!-- Visual Studio Browser Lin ...

  4. Mac 下查看网络端口占用情况

    1.Mac 下查看网络端口占用情况 有的时候关闭了服务器,但是端口还是占用,解决的方法是 kill 掉占用该端口的进程. # 查看 8009 端口的占用情况 $ lsof -i:8009 可以看到,该 ...

  5. 安装gitlab遇到的问题

    参考文章: http://www.pickysysadmin.ca/2013/03/25/how-to-install-gitlab-5-0-on-centos-6/ 一直跟着这篇文章做,还挺顺利的, ...

  6. 转: nginx使用image_filter生成缩略图 -- fasdfs海量图片缩略图整合

      转: nginx使用image_filter生成缩略图 -- fasdfs海量图片缩略图整合 http://blog.csdn.net/CleverCode/article/details/522 ...

  7. Python 爬虫实例(14) 爬取 百度音乐

    #-*-coding:utf-8-*- from common.contest import * import urllib def spider(): song_types = ['新歌','热歌' ...

  8. linux php7.1 安装redis扩展

    redis扩展下载网址 http://pecl.php.net/package/redis wget http://101.96.10.64/pecl.php.net/get/redis-3.1.3. ...

  9. KMP算法理解

    1.KMP算法解决问题:对BF(Brute Force)算法优化,避免对主串进行回溯匹配(匹配不成功主串指针向后移1位,子串指针重置开始位置,两串继续匹配),效率底. 2.KMP算法原则/目的:主串不 ...

  10. MySQL 自定义函数CREATE FUNCTION实例

    分享一个MySQL 自定义函数CREATE FUNCTION的实例.mysql> delimiter $$mysql> CREATE FUNCTION myFunction-> (i ...