美文网首页
python tcp

python tcp

作者: 极速魔法 | 来源:发表于2018-01-02 20:47 被阅读13次

python tcp

http://www.binarytides.com/python-socket-programming-tutorial/
https://www.jianshu.com/p/e062b3dd110c
https://www.liaoxuefeng.com

server.py

'''
open socket
bind (add,port)
listen for incoming connections
accept connections
receive/send

Accept a connection. The socket must be bound to an address and listening for connections.
The return value can be either None or a pair (conn, address)
where conn is a new socket object usable to send and receive data on the connection,
and address is the address bound to the socket on the other end of the connection.
'''
import socket
import sys
import threading
import time

host='127.0.0.1'
port=9999

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('server socket created')

try:
    s.bind((host,port))
except socket.error:
    print('bind failed')
    sys.exit()
print('bind complete')

#listen
s.listen(10)
print('start listen...')


def tcplink(sock, addr):
    print('accept new connection {0}:{1}'.format(addr[0],str(addr[1])))
    #send message to connected client
    sock.send(b'welcome to server!')
    while True:
        #receive btye
        data=sock.recv(1024)
        msg='hello '+data.decode('utf-8')
        #time.sleep(1)
        if not data:
            break
        sock.sendall(msg.encode('utf-8'))
    sock.close()
    print('conection from {0}:{1} closed'.format(addr[0],addr[1]))

while True:
    #accept new connection
    sock,addr=s.accept()
    t=threading.Thread(target=tcplink,args=(sock,addr))
    t.start()

client.py

import socket
import sys

host='localhost'
port=9999

try:

    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error:
    print('socket create failed')
    sys.exit()

s.connect((host,port))
print(s.recv(1024).decode('utf-8'))

while True:
    data=input('please input some string\n')
    if not data or data=='exit':
        break

    s.sendall(str(data).encode('utf-8'))
    ret=s.recv(1024)
    print(ret.decode('utf-8'))
s.close()

相关文章

  • Python TCP编程

    Python网络编程之TCP 一、TCP协议 TCP协议,传输控制协议(Transmission Control ...

  • python tcp

    python tcp http://www.binarytides.com/python-socket-progr...

  • 【Python入门】39.网络编程

    摘要:网络编程的概念介绍;TCP/IP协议的基本介绍;介绍Python的socket库,在Python中进行TCP...

  • Python-socket模拟TCP通讯

    Python socket模拟TCP通讯 本文将介绍使用Python的socket模块创建TCP客户端和服务端的方...

  • [python|Go] python与Go之间的tcp通信-传输

    python与Go之间的tcp通信-传输json消息 python作为服务端, tcp作为客户端 传输多条json...

  • socket编程

    python实现socket编程 实现的主要过程是: tcp server: tcp client 函数 的实现原型:

  • 扣丁学堂Python开发之udp和tcp协议详解

    今天扣丁学堂Python培训老师给大家介绍一下关于Python基础开发中udp和tcp协议详解,TCP和UDP是O...

  • tcp和udp

    【Python网络编程】利用Python进行TCP、UDP套接字编程 http://www.cnblogs.com...

  • Python TCP

    利用 python 的 socket 模块可以实现基本的网络编程,并且只限于一对一的连接。当然,也可以在其基础上实...

  • 使用python进行tcp协议接口的并发测试

    进行 tcp 协议的接口测试 首先要了解tcp协议 以及可以使用python实现tcp协议如果之前没有掌握 可以查...

网友评论

      本文标题:python tcp

      本文链接:https://www.haomeiwen.com/subject/sykagxtx.html