码迷,mamicode.com
首页 > 编程语言 > 详细

python tcp

时间:2019-01-24 14:39:21      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:obj   imp   dump   hello   import   style   class   connect   not   

 

server

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

输出:

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

 


 

client

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 28 22:40:41 2016

@author: zhanghc
"""

import socket
import json
import time
host="localhost"
port=10000
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
data={"req":"get","obj":"uuid","seq":0,"param":{}}
j = json.dumps(data)
s.send(j)
print(s.recv(1024))
s.close()

输出

Hello!

 

 

python tcp

标签:obj   imp   dump   hello   import   style   class   connect   not   

原文地址:https://www.cnblogs.com/sea-stream/p/10313975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!