Python - API Phyton

   
Vista:

API Phyton

Publicado por pepe (1 intervención) el 20/05/2015 11:53:22
Hola a todos

Necesitaría de vuestra ayuda para terminar de crear un script en phyton para modificar por API ciertas variables.
No soy ducho en programación y me he atascado.

Tengo un script para obtener datos, pero necesito otro para añadir por el API

Esto es lo que he creado, pero no funciona y no tengo mensajes de cual es el error o que falta.
..................
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
 
import os,sys,string
import httplib, urllib
import json
 
# Variables globales estaticas
SERVER = "my.site.com"
PAGE_SIZE = 125
PAGE_NUM = 0
 
 
URI = "/api/prov/v1/sites/configure/acl"
api_id = "xxxxx"
api_key = "xxxxxxxxxx"
site_id = "xxxxx"
rule_id = "api.acl.blacklisted_ips"
ips = "44.44.44.44,66.66.66.66"
..................
 
 
Este es el script que recopila información y en el que me he basado, sabiendo que funciona:
..................
#!/usr/bin/python
 
import os,sys,string
import httplib, urllib
import json,requests
 
# Variables globales estaticas
SERVER_INCAPSULA = "my.incapsula.com"
PAGE_SIZE = 125
PAGE_NUM = 0
 
def getBandwidthSite(site_id):
 
  c = httplib.HTTPSConnection(SERVER)
  if (time_range == "custom"):
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'bandwidth_timeseries','time_range':time_range,'start':start_time,'end':end_time})
  else :
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'bandwidth_timeseries','time_range':time_range})
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  c.request("POST", "/api/stats/v1",params,headers)
  response = c.getresponse()
  data = response.read()
  c.close()
  json_data = json.loads(data)
  total = 0
 
  for i in json_data["bandwidth_timeseries"][0]["data"]:
    cont = 0
    for j in i:
       cont+=1
       if (cont%2 == 0):
            total=total+j
            #print "total bytes:%s" %total
 
  return total
 
 
def getSitesList():
 
  # Devuelve una lista con los sites que hay en esa cuenta
  # con los valores de api_id y api_key ya sabe el account_id
  URI = "/api/prov/v1/sites/list"
  c = httplib.HTTPSConnection(SERVER)
  params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'page_size':PAGE_SIZE,'page_num':PAGE_NUM})
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  c.request("POST", URI,params,headers)
  response = c.getresponse()
  data = response.read()
  c.close()
  json_data = json.loads(data)
  #  print "Site_id = %s" %json_data["sites"][1]["site_id"]
  #  print "Domain = %s" %json_data["sites"][1]["domain"]   
  site_id_list= []
  domain_list = []
  for i in json_data["sites"]:
     #print "Site_id => %s" %i["site_id"]
     #print "Domain => %s" %i["domain"]
     site_id_list.append(i["site_id"])
     domain_list.append(i["domain"])
  return site_id_list,domain_list
 
 
def getVisits(site_id):
 
  c = httplib.HTTPSConnection("my.incapsula.com")
  if (time_range == "custom"):
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'visits_timeseries','time_range':time_range,'start':start_time,'end':end_time})
  else :
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'visits_timeseries','time_range':time_range})
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  c.request("POST", "/api/stats/v1",params,headers)
  response = c.getresponse()
  data = response.read()
  c.close()
  json_data = json.loads(data)
  visitas_humanas = 0
 
  for i in json_data["visits_timeseries"][0]["data"]:  # visitas humanas
    cont = 0
    for j in i:
       cont+=1
       if (cont%2 == 0):
            visitas_humanas=visitas_humanas+j
            #print "total visitas humanas :%s" %visitas_humanas
 
  visitas_bots = 0
  for i in json_data["visits_timeseries"][1]["data"]:  # visitas bots
    cont = 0
    for j in i:
       cont+=1
       if (cont%2 == 0):
            visitas_bots=visitas_bots+j
            #print "total visitas bots :%s" %visitas_bots
 
 
  return visitas_humanas,visitas_bots,visitas_humanas+visitas_bots
 
 
 
def getHits(site_id):
 
  c = httplib.HTTPSConnection("my.site.com")
 
  if (time_range == "custom"):
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'hits_timeseries','time_range':time_range,'start':start_time,'end':end_time})
  else :
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'hits_timeseries','time_range':time_range})
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  c.request("POST", "/api/stats/v1",params,headers)
  response = c.getresponse()
  data = response.read()
  c.close()
  json_data = json.loads(data)
  hits_humanos = 0
 
  for i in json_data["hits_timeseries"][0]["data"]:  # hits humanos
    cont = 0
    for j in i:
       cont+=1
       if (cont%2 == 0):
            hits_humanos=hits_humanos+j
            #print "total hits humanos :%s" %hits_humanos
 
  hits_bots = 0
  for i in json_data["hits_timeseries"][1]["data"]:  # hits bots
    cont = 0
    for j in i:
       cont+=1
       if (cont%2 == 0):
            hits_bots=hits_bots+j
           # print "total hits bots :%s" %hits_bots
 
 
  return hits_humanos,hits_bots,hits_humanos+hits_bots
 
 
def generateCSVReport():
  file = open("./report.csv","w")
  file.write("Site,Human Visits,Bot Visits,Total Visits,Hits,Bandwidth (MB),SQL Injection,Illegal Resource Access,Cross Site Scripting,Bad Bots,DDoS\n")
  j = 0
  for site in lista_sites[0]:
     #print "Site: %s (%s) => Ancho de banda (%s)" %(lista_sites[1][j],site,getBandwidthSite(site))
     visitas_list = getVisits(site)
     hits_list = getHits(site)
     threat_list = getThreats(site)
     bandwidth = round(getBandwidthSite(site)/float(1024*1024),2)
     domain = lista_sites[1][j]
     file.write(domain+" (" + str(site) + ")," + str(visitas_list[0]) + "," + str(visitas_list[1]) + "," + str(visitas_list[2]) + "," + str(hits_list[2]) + "," + str(bandwidth) + ","  + str(threat_list[0]) + "," + str(threat_list[1]) + "," + str(threat_list[2]) + "," + str(threat_list[3]) + "," + str(threat_list[4]) + "\n")
     j+=1
     print "%s/%s Sites Completados: %s " %(j,len(lista_sites[0]),lista_sites[1][j-1])
 
  file.close()
  return
 
 
def getThreats(site_id):
 
  c = httplib.HTTPSConnection("my.site.com")
  if (time_range == "custom"):
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'threats','time_range':time_range,'start':start_time,'end':end_time})
  else :
     params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'threats','time_range':time_range})
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  c.request("POST", "/api/stats/v1",params,headers)
  response = c.getresponse()
  data = response.read()
  c.close()
  json_data = json.loads(data)
  sql_injection = 0
  illegal_resource = 0
  xss = 0
  bad_bots = 0
  ddos = 0
 
 
 
#  print "XSS -> %s" %xss
  return sql_injection,illegal_resource,xss,bad_bots,ddos
 
 
 
if __name__ == "__main__" :
 
  api_id = "xxxxxx"
  api_key = "xxxxxxxxxxxxx"
 
 
  # 1 de Enero 2015 a las 00:00:00h en milisegundos
  #start_time = "1420070400000" 
  # 31 de Enero 2015 a las 23.59.59h en milisegundos
  #end_time = "1422748799000"
  start_time = "1428883200000"
  end_time = "1429487999000"
  time_range = "custom"
  lista_sites = getSitesList()
  generateCSVReport()
 
  # Probar un site especifico
  ##visitas = getVisits("1872262")
  ##print "Visitas humanas => %s " %visitas[0]
  ## print "Visitas bots => %s " %visitas[1]
  ##print "Total Visitas => %s " %visitas[2]
 
 
  #  print "lista de dominios => %s" %lista_sites[1]i
  #  print "site id => %s " %lista_sites[0]i
  #j = 0
  #for site in lista_sites[0]:
  #   print "Site: %s (%s) => Ancho de banda (%s)" %(lista_sites[1][j],site,getBandwidthSite(site))
  #   j+=1
 
 
  #for site in lista_sites[0]:
  #    visitas_list = getVisits(site)
  #    print "Visitas humanas => %s " %visitas_list[0]
  #    print "Visitas bots => %s " %visitas_list[1]
  #    print "Total Visitas => %s " %visitas_list[2]
 
  #hits_list = getHits(site)
  #for site in lista_sites[0]:
  #    print "Hits humanos => %s " %hits_list[0]
  #    print "Hits bots => %s " %hits_list[1]
  #    print "Hits Totales => %s " %hits_list[2]
 
  #threats_list = getThreats(site)
  #for site in lista_sites[0]:
  #    print "SQLi => %s " %threats_list[0]
  #    print "Illegal Resource => %s " %threats_list[1]
  #    print "XSS => %s " %threats_list[2]
  #    print "Bad Bots => %s " %threats_list[3]

.................
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder