python tcp

server

import socket
host="localhost"
port=
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen()
while():
sock,addr=s.accept()
print("got connection form ",sock.getpeername())
sock.send('Hello!')
data=sock.recv()
if not data:
print("no client")
else:
print("cdcd",data)
data=sock.recv()
if not data:
print("no client")
else:
print(data)
data=sock.recv()
if not data:
print("no client")
else:
print(data)

输出:

('got connection form ', ('127.0.0.1', ))
('cdcd', '{"req": "get", "obj": "uuid", "param": {}, "seq": 0}')
no client
no client

client

# -*- coding: utf- -*-
"""
Created on Mon Mar :: @author: zhanghc
""" import socket
import json
import time
host="localhost"
port=
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
data={"req":"get","obj":"uuid","seq":,"param":{}}
j = json.dumps(data)
s.send(j)
print(s.recv())
s.close()

输出

Hello!
上一篇:Swift - 32 - 函数类型


下一篇:Java采用RSA加密及解密技术的有关Maven项目的配置流程: