python使用socket向客户端发送数据的方法

在使用locust测试长连接的时候,所有的请求全部faillure了,所以想到手动写一个连接脚本测试一下是否能连通

因为centos7自带python2.7所以用python写一个比较方便。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
import pdb
import datetime
HOST='192.168.11.11'
PORT=9000
teststr=''

from multiprocessing import Pool

def fun():
    print(datetime.datetime.now())
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    try:
        s.sendall(teststr)
        data = s.recv(1024)
        print(str(data))
        s.close()
        print(datetime.datetime.now())
    except KeyboardInterrupt:
        print('
')

if __name__ == '__main__':
    p = Pool(1)
    for i in range(1):
        p.apply_async(fun)
    p.close()
    p.join()
原文地址:https://www.cnblogs.com/mingyue5826/p/11532196.html