0

I have a problem with my python script.

I want to send information when the client is connected to the server.

I have 9 lists for saving data:

PLATFORM = []
PLATFORM_RELEASE = []
PLATFORM_ARCH = []
USER_ACCOUNT = []
COMPUTER_ACCOUNT = []
COUNTRY = []
CITY = []
LATITUDE_LONGITUDE = []
ORG = []

In my client:

URL = "http://ipinfo.io/json"
Response = urllib2.urlopen(URL)
Reading_data = json.load(Response)

COUNTRY = str(Reading_data['country'])
CITY = str(Reading_data['city'])
LATITUDE_LONGITUDE = str(Reading_data['loc'])
ORG = str(Reading_data['org'])

PLATFORM = platform.uname()[0] 
PLATFORM_RELEASE = platform.uname()[2]
PLATFORM_ARCH = platform.uname()[4]
USER_ACCOUNT = os.getlogin()
COMPUTER_ACCOUNT = platform.uname()[1] 

try:
    server.send(str.encode(PLATFORM))
    server.send(str.encode(PLATFORM_RELEASE))
    server.send(str.encode(PLATFORM_ARCH))
    server.send(str.encode(USER_ACCOUNT))
    server.send(str.encode(COMPUTER_ACCOUNT))
    server.send(str.encode(COUNTRY))
    server.send(str.encode(CITY))
    server.send(str.encode(LATITUDE_LONGITUDE))
    server.send(str.encode(ORG))
    print "sending complete"
except:
    print "sending information error"

In my server:

try:
        PLATFORM.append(Connection.recv(1024))
        PLATFORM_RELEASE.append(Connection.recv(1024))
        PLATFORM_ARCH.append(Connection.recv(1024))
        USER_ACCOUNT.append(Connection.recv(1024))
        COMPUTER_ACCOUNT.append(Connection.recv(1024))
        COUNTRY.append(Connection.recv(1024))
        CITY.append(Connection.recv(1024))
        LATITUDE_LONGITUDE.append(Connection.recv(1024))
        ORG.append(Connection.recv(1024))
        return
    except:
        print "error"

My server output:

['Linux4.16.0-kali2-amd64x86_64']
['rootBobby']
['NLPenningsveer52.3922,4.6786AS13213 UK-2 Limited']
[]
[]
[]
[]
[]
[]

I need this output:

['Linux']
['4.16.0-kali2-amd64']
['x86_64']
['root']
['Bobby']
['NL']
['Penningsveer']
['52.3922,4.6786']
['AS13213 UK-2 Limited']

What is my problem? Why is this happening?

NOTE: I am using VPN

Thank you

Mufeed
  • 3,018
  • 4
  • 20
  • 29
D.H
  • 37
  • 1
  • 7
  • Because you're reading 1024 bytes each time. The data you send is less than 1024 bytes, so you get data from the next send call. Why don't you send all the info as a json string? – t.m.adam Jul 17 '18 at 09:01

2 Answers2

1

t.m.adam already told in his comment what the problem is. An other solution than the one he suggested is, since your strings don't contain newline characters, to send and receive them line by line. So, you could replace all the server.send(str.encode(…)) statements with

    server.makefile().write('\n'.join([PLATFORM, PLATFORM_RELEASE,
                                       PLATFORM_ARCH, USER_ACCOUNT,
                                       COMPUTER_ACCOUNT, COUNTRY, CITY,
                                       LATITUDE_LONGITUDE, ORG]))

and all the ….append(Connection.recv(1024)) statements with

f = Connection.makefile()
for a in [PLATFORM, PLATFORM_RELEASE, PLATFORM_ARCH, USER_ACCOUNT,
          COMPUTER_ACCOUNT, COUNTRY, CITY, LATITUDE_LONGITUDE, ORG]:
    a.append(f.readline().rstrip())
f.close()
Armali
  • 18,255
  • 14
  • 57
  • 171
0

Here's a possible approach:

Client

import json
import os
import platform
import requests

response = requests.get('http://ipinfo.io/json').json()

d = {
    "PLATFORM": platform.uname()[0],
    "PLATFORM_RELEASE": platform.uname()[2],
    "PLATFORM_ARCH": platform.uname()[4],
    "USER_ACCOUNT": os.getlogin(),
    "COMPUTER_ACCOUNT": platform.uname()[1],
    "COUNTRY": [response['country']],
    "CITY": [response['city']],
    "LATITUDE_LONGITUDE": [response['loc']],
    "ORG": [response['org']],
}

data = json.dumps(d).encode('utf-8')

server.sendall(data)

Server

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 1234))
s.listen(1)
conn, addr = s.accept()
b = b''
while 1:
    tmp = conn.recv(1024)
    b += tmp
d = json.loads(b.decode('utf-8'))
print(d)

Most parts of the answer taken from here.

Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25