写一段python代码,获取网络实时时间
时间: 2024-02-18 15:53:49 浏览: 22
以下是获取网络实时时间的Python代码:
```python
import urllib.request
import json
url = 'http://worldtimeapi.org/api/timezone/Asia/Shanghai'
response = urllib.request.urlopen(url)
json_data = json.loads(response.read().decode())
time = json_data['datetime']
print(time)
```
运行结果类似于:`2021-08-06T10:18:35.363033+08:00`,这是现在在上海的时间。这段代码从`worldtimeapi.org`获取了实时的时间信息,解析后输出了时间。
相关问题
写一段用scapy获取数据包时间的python代码
以下是获取数据包时间的 Python 代码:
```python
from scapy.all import *
def packet_handler(packet):
print(packet.time)
sniff(prn=packet_handler, count=10)
```
这段代码使用了 Scapy 库,通过 `sniff` 函数捕获了 10 个数据包,并在 `packet_handler` 函数中打印出每个数据包的时间戳。
写一段获取天气Python代码
以下是一个获取天气的Python代码示例,需要使用第三方库requests和json:
```python
import requests
import json
# 输入城市名
city = input("请输入城市名:")
# API地址
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
# 发送请求并获取响应
response = requests.get(url)
# 将响应内容转换为JSON格式
data = json.loads(response.text)
# 提取天气信息
if data.get("desc") == "OK":
forecast = data.get("data").get("forecast")[0]
date = forecast.get("date")
high = forecast.get("high")
low = forecast.get("low")
type = forecast.get("type")
print(f"{city} {date} 天气:{type},最高温度:{high},最低温度:{low}")
else:
print(data.get("desc"))
```
运行代码后,程序会要求输入城市名,然后会返回该城市当天的天气信息。