C114门户论坛百科APPEN| 举报 切换到宽版

亚星游戏官网

 找回密码
 注册

只需一步,快速开始

短信验证,便捷登录

搜索
查看: 3489|回复: 1

[技术讨论] Python建立socket测试 [复制链接]

军衔等级:

亚星游戏官网-yaxin222  上士

注册:2008-9-20
发表于 2013-7-14 15:02:07 |显示全部楼层
嵌入式Web 服务器,需要压力测试,正好可以使用Python快速写一个小脚本,只花了半天时间,推荐给大家,继续完善,可以多线程发包,长连接或者短连接。
import socket #for sockets
import sys #for exit
import time
import thread,threading  
remote_ip = '60.1.21.163'
port = 80
sockIndex = 1
def connToServer (x,times):
    global sockIndex   
    Reconnect = 0
    try:
        #create an AF_INET, STREAM socket (TCP)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((remote_ip , port))
    except socket.error:
        print ('Failed to create socket. Error code: ')
        sys.exit();

    for i in range(1, times):
        try:
            #global Reconnect
            if(1 == Reconnect):
                print('Reconnect')
                s.close
                time.sleep(1)
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((remote_ip , port))
                print('Reconnect success')
                Reconnect = 0
            #s.sendall('POST /OMC_TOOL?action=UDT/ACS_OAM_CTRL HTTP/1.1\r\n')
            #s.sendall('GET /OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\nconnection:  keep-alive\r\n\r\n')
            s.sendall('GET /OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            s.sendall('GET /OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /5OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /6OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /7OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /8OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            #s.sendall('GET /9OMC_TOOL?action=UDT/ACS_OAM_DATA_REPORT HTTP/1.1\r\n\r\n')
            
            print ('socket s Senddata Successfully!')
            time.sleep(1)
        except socket.error,arg:
            #global Reconnect
            Reconnect =1   
            (errno,err_msg) = arg
            print"fail:%s,errno=%d" %(err_msg,errno)
            s.close
def test(): #Use thread.start_new_thread() to create 2 new threads  
    thread.start_new_thread(connToServer,(1,10000))  
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))
    thread.start_new_thread(connToServer,(1,10000))      
   
if __name__=='__main__':  
    test()  
print ('Message test over')


举报本楼

本帖有 1 个回帖,您需要登录后才能浏览 登录 | 注册
您需要登录后才可以回帖 登录 | 注册 |

手机版|C114 ( 沪ICP备12002291号-1 )|联系大家 |网站地图  

GMT+8, 2024-11-15 22:20 , Processed in 0.171435 second(s), 15 queries , Gzip On.

Copyright © 1999-2023 C114 All Rights Reserved

Discuz Licensed

回顶部
XML 地图 | Sitemap 地图