您的位置:首页 > 编程语言 > Python开发

python基础(json,socket)

2015-12-01 16:21 344 查看
http://python-data.dr-chuck.net/geojson?sensor=false&address=Vilnius+University

{
"results" : [
{
"address_components" : [
{
"long_name" : "Vilnius",
"short_name" : "Vilnius",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Vilnius city municipality",
"short_name" : "Vilnius city municipality",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Vilnius County",
"short_name" : "Vilnius County",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Lithuania",
"short_name" : "LT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Vilnius, Lithuania",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 54.832544,
"lng" : 25.4814883
},
"southwest" : {
"lat" : 54.567798,
"lng" : 25.024376
}
},
"location" : {
"lat" : 54.6871555,
"lng" : 25.2796514
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 54.832544,
"lng" : 25.4814883
},
"southwest" : {
"lat" : 54.567798,
"lng" : 25.024376
}
}
},
"partial_match" : true,
"place_id" : "ChIJ9QhkXPuT3UYRQNzpcIzRAAQ",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}


import urllib
import json

url = 'http://python-data.dr-chuck.net/geojson?'

while True:
address = raw_input()
serviceUrl = url + urllib.urlencode({'sensor': 'false', 'address': address})
print serviceUrl
input = urllib.urlopen(serviceUrl).read()
js = json.loads(input)
print js['results'][0]['place_id']



http://python-data.dr-chuck.net/comments_194528.json
{
"note":"This file contains the actual data for your assignment</note>",
"comments":[
{
"name":"Anastasija",
"count":100
},
{
"name":"Ridwan",
"count":98
},
{
"name":"Rory",
"count":88
},
{
"name":"Aeryn",
"count":88
},
{
"name":"Marcus",
"count":84
},
{
"name":"Jock",
"count":83
},
{
"name":"Talise",
"count":81
},
{
"name":"Rheanne",
"count":80
},
{
"name":"Wardah",
"count":78
},
{
"name":"Karyss",
"count":74
},
{
"name":"Mea",
"count":70
},
{
"name":"Maryk",
"count":69
},
{
"name":"Neshawn",
"count":69
},
{
"name":"Miyah",
"count":63
},
{
"name":"Janelle",
"count":63
},
{
"name":"Finnen",
"count":60
},
{
"name":"Michela",
"count":59
},
{
"name":"Yishuka",
"count":57
},
{
"name":"Kern",
"count":57
},
{
"name":"Tanner",
"count":57
},
{
"name":"Zhong",
"count":56
},
{
"name":"Merin",
"count":54
},
{
"name":"Jian",
"count":53
},
{
"name":"Khadijah",
"count":52
},
{
"name":"Lauren",
"count":49
},
{
"name":"Adam",
"count":47
},
{
"name":"Akan",
"count":43
},
{
"name":"Shauni",
"count":43
},
{
"name":"Klein",
"count":40
},
{
"name":"Brett",
"count":38
},
{
"name":"Ayat",
"count":37
},
{
"name":"Rayna",
"count":35
},
{
"name":"Duaa",
"count":34
},
{
"name":"Mariyah",
"count":32
},
{
"name":"Matthias",
"count":30
},
{
"name":"Olurotimi",
"count":26
},
{
"name":"Odynn",
"count":23
},
{
"name":"Shahna",
"count":21
},
{
"name":"Saphyre",
"count":20
},
{
"name":"Bowie",
"count":19
},
{
"name":"Keera",
"count":18
},
{
"name":"Ross",
"count":14
},
{
"name":"Caragh",
"count":13
},
{
"name":"Siena",
"count":12
},
{
"name":"Carley",
"count":9
},
{
"name":"Waqaas",
"count":9
},
{
"name":"Kira",
"count":9
},
{
"name":"Avinash",
"count":3
},
{
"name":"Alysia",
"count":1
},
{
"name":"Josephina",
"count":1
}
]
}


import urllib
import json

url = 'http://python-data.dr-chuck.net/comments_194528.json'

input = urllib.urlopen(url).read()
js = json.loads(input)
sum = 0
for dict in js['comments']:
sum += int(dict['count'])
print sum


import socket
import time
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('news.xinhuanet.com', 80))
mysock.send('GET http://news.xinhuanet.com/photo/2015-12/01/128485250_14489244150011n.jpg HTTP/1.0\n\n')
count = 0

picture = "";
while True:
data = mysock.recv(5120)
if ( len(data) < 1 ) : break
# time.sleep(0.25)
count = count + len(data)
print len(data),count
picture = picture + data
mysock.close()
# Look for the end of the header (2 CRLF)
pos = picture.find("\r\n\r\n")
print 'Header length',pos
print picture[:pos]
# Skip past the header and save the picture data
picture = picture[pos+4:]
fhand = open("stuff.jpg","wb")
fhand.write(picture)
fhand.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: