我的python脚本有问题。
我想在客户端连接到服务器时发送信息。
我有9个保存数据的列表:
PLATFORM = []
PLATFORM_RELEASE = []
PLATFORM_ARCH = []
USER_ACCOUNT = []
COMPUTER_ACCOUNT = []
COUNTRY = []
CITY = []
LATITUDE_LONGITUDE = []
ORG = []
在我的客户中:
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"
在我的服务器中:
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"
我的服务器输出:
['Linux4.16.0-kali2-amd64x86_64']
['rootBobby']
['NLPenningsveer52.3922,4.6786AS13213 UK-2 Limited']
[]
[]
[]
[]
[]
[]
我需要以下输出:
['Linux']
['4.16.0-kali2-amd64']
['x86_64']
['root']
['Bobby']
['NL']
['Penningsveer']
['52.3922,4.6786']
['AS13213 UK-2 Limited']
我的问题是什么? 为什么会这样?
注意:我正在使用VPN
谢谢
答案 0 :(得分:1)
t.m.adam已经在评论中告诉了问题所在。除了他建议的解决方案之外,另一种解决方案是,由于您的字符串不包含换行符,因此可以逐行发送和接收它们。因此,您可以将所有server.send(str.encode(…))
语句替换为
server.makefile().write('\n'.join([PLATFORM, PLATFORM_RELEASE,
PLATFORM_ARCH, USER_ACCOUNT,
COMPUTER_ACCOUNT, COUNTRY, CITY,
LATITUDE_LONGITUDE, ORG]))
以及所有….append(Connection.recv(1024))
语句
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()
答案 1 :(得分:0)
这是一种可能的方法:
客户
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)
服务器
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)
答案的大部分来自here。