python slow in socket operations -
i have server written in java , client written in python. when run client script(it creates 10k clients sending data infinitely),majority of time spent in socket creation , connection. have server running on system connected through lan. when run java client program, time taken 3-6 times less python. problem of python? or there problem in code?
here program reference.
import array import os import sys import encodings import subprocess import datetime import binascii import string import random import time import threading import socket p=input("enter no of client...") host=raw_input("enter host name") host=socket.gethostbyname(host) port=input("enter port no......") f1=open("xys.txt","a") f2=open("string.txt","a") def id_generator(length,a,b): # generates unique random values between given range random.seed() while(1): x=''.join(random.choice(a+b) _ in range(length)) if(a<=x<=b): return x def rand_string(): a=[] s1="29298000280a81d72b150314234153026368560793189500000394f88fd18fffff0000001e0000000000000a0d" f=open("rule1.txt","r") k=f.readline() k=k.rstrip('\n') if(k=="h"): try: a=[] b=bytearray.fromhex(s1) i=0 k=f.readline() k=k.rstrip('\n') ## reads no of arguments z=int(k) n=0 k=f.readline() k=k.rstrip('\n') #reads delimiter.... while(n<z): k=f.readline() k=k.rsplit('\n') # reads arguments c="".join(k) # converting list strings a=c.rsplit('|') # split argument # first arg index of bit changed(e.g. 10 means 11th bit) # second arg no of bits changed # next field name value randomized # next no of unique random values generated # last 2 range b/w value should lie i=0 while(i<int(a[3])): b=bytearray.fromhex(s1) al=''.join(a[4]) ah=''.join(a[5]) x=id_generator(int(a[1]),al,ah) b1=bytearray.fromhex(x) m=int(a[0])/2 l=0 while(m<(int(a[0])/2)+int(a[1])/2): b[m]=b1[l] m=m+1 l=l+1 t=binascii.hexlify(bytearray(b)) d=0 y=list(t) while(d<len(y)): y[d]=y[d].upper() d=d+1 s3=''.join(y) i=i+1 s1=s3 n=n+1 f.close() return (s3) except exception: return ("sorry") class procthread(threading.thread): def __init__(self,id): threading.thread.__init__(self) self.id=id def run(self): try: s=socket.socket(socket.af_inet, socket.sock_stream) s.connect((host,port)) print self.id, print 'socket' t=rand_string() s.send(str(t)) x=t while(1): t=rand_string() x=list(x) t=list(t) in range(10,18): t[i]=x[i] x=''.join(x) s3=''.join(t) f2.write(str(self.id)+'\t'+s3+'\n'+'\n') m=s.recv(65536) m=binascii.hexlify(bytearray(m)) f1.write(str(m)+'\n'+'\n') t1=threading.event() t1.wait(timeout=float(5)) s.send(s3) i=0 except socket.error: f5=open("error.txt","a") f5.write(str(self.id)+'\n') f5.close() in range(1,p+1): if((i%100)==0): t=threading.event() t.wait(timeout=random.uniform(1,3)+1) pt=procthread(i) pt.start()
Comments
Post a Comment