title: wanted to improve python socket processing
body: <p>The requirement is to receive <em>all</em> traffic from port1(p1p1) and then encapsulate those packets to udp port xxx and send it out on port2 on 5 different source ips.</p>
  <pre><code>import socket
  import struct
  import sys
  import time
  import re
  import random
  from collections import namedtuple
  
  
  def get_socket_connection():
      global sw_list
      udp_port = int('xxx')
  
      for i in range(0, 5):
          IP = '10.10.1.%d'%(1+i)
          try:
              #socket connections to send out the encapsulated traffic
              globals()[f&quot;send_sock_{i}&quot;] = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
              globals()[f&quot;send_sock_{i}&quot;].bind((IP, 0))
          except socket.error as msg:
              print('Socket could not be created for IP %s. Error Code : %s ' %(IP, str(msg)))
              sys.exit()
  
      try:
          #socket connection to receive raw/native traffic
          s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
      except socket.error as msg:
          print('Socket could not be created to read the traffic. Error Code : %s ' %(str(msg)))
          sys.exit()
  
      # receive a packet
      while True:
          packet = s.recvfrom(65565)
  
          #receive traffic ONLY from port p1p1 otherwise DO NOT PROCESS
  
          if packet[1][0] != 'p1p1':
              continue
  
          packet = packet[0]
          num = random.randint(0, 4)
          globals()[f&quot;send_sock_{num}&quot;].sendto(header_get(sw_list[num].port, sw_list[num].dpid, packet), (str('1.1.1.1'), int(udp_port)) )
  
  def mac_to_int(mac):
      res = re.match('^((?:(?:[0-9a-f]{2}):){5}[0-9a-f]{2})$', mac.lower())
      if res is None:
          raise ValueError('invalid mac address')
      #print(int(res.group(0).replace(':', ''), 16))
      return int(res.group(0).replace(':', ''), 16)
  
  def header_get(port, dpid, data_packet):
      global iteration
      pkt=struct.pack(&quot;!2I2Q&quot;, 2, port, dpid, round(time.clock_gettime(time.CLOCK_REALTIME)))
      iteration += 1
      print(iteration)
      return (pkt+data_packet)
  
  def main():
  
      global proto_dic, iteration, sw_list
  
      iteration = int(0)
      sw = namedtuple(&quot;sw&quot;, [&quot;num&quot;, &quot;sw_name&quot;, &quot;dpid&quot;, &quot;port&quot;])
      sw_list = []
      count = int(0)
  
      sw_list.append(sw(count+1, &quot;s1&quot;, mac_to_int('3c:2c:30:5c:61:80'), int(1)))
      sw_list.append(sw(count+2, &quot;s2&quot;, mac_to_int('98:5d:82:c1:a0:80'), int(1)))
      sw_list.append(sw(count+3, &quot;s3&quot;, mac_to_int('20:04:0f:2c:cf:42'), int(1)))
      sw_list.append(sw(count+4, &quot;s4&quot;, mac_to_int('e4:f0:04:32:94:1a'), int(1)))
      sw_list.append(sw(count+5, &quot;s5&quot;, mac_to_int('3c:2c:99:69:2e:2c'), int(1)))
      sw_list.append(sw(count+6, &quot;s5&quot;, mac_to_int('3c:2c:99:69:2e:2c'), int(2)))
  
      get_socket_connection()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>As such the above code is working fine however, it is unable to process more than 20k pps guaranteed rate. I'm looking for atleast 75K guaranteed rate so I would like to know what optimizations can to be done to get such performances? This server has 4 CPU core and 16G ram.</p>
  <p>Appreciate any help.</p>