Python socket recv buffer handling - python

Im attempting to speed up socket handling in python using a buffer/queue
below an implementation for cyclic buffer I use to read max size out of localhost socket used for IPC
class BufferHandling:
def __init__(self, buffer_size, max_chunk_size):
self._read_index = 0
self._write_index = 0
self._buffer = bytearray(buffer_size)
self._mem_view = memoryview(self._buffer)
self._max_size = max_chunk_size
def fill_from_socket(self, _socket):
logging.debug("2 fill: write index %s , read_index %s", self._write_index , self._read_index)
if self._read_index > self._write_index:
if self._read_index - self._write_index < self._max_size:
raise RuntimeError("no place to write in buffer")
ret = _socket.recv_into(self._mem_view[self._write_index:], self._max_size)
self._write_index += ret
#logging.debug("1 recv_from return with %s, write index %s", ret, self._write_index)
return
# write >= read
_1st_write_size = len(self._mem_view) - self._write_index
_2nd_write_size = self._read_index
if _1st_write_size + _2nd_write_size < self._max_size:
raise RuntimeError("no place to write in buffer")
if _1st_write_size >= self._max_size:
ret = _socket.recv_into(self._mem_view[self._write_index:], self._max_size)
self._write_index += ret
#logging.debug("2 recv_from return with %s , write index %s", ret, self._write_index)
return
# clear some room ...
self._mem_view[:self._write_index - self._read_index] = self._mem_view[self._read_index: self._write_index]
self._write_index = self._write_index - self._read_index
self._read_index = 0
ret = _socket.recv_into(self._mem_view[self._write_index:], self._max_size)
self._write_index += ret
logging.debug("3 recv_from return with %s, write index %s", ret, self._write_index)
"""
# read data from cyclic buffer
# if data found , return data according to _to_read size
# else return empty string
"""
def read_from_buffer(self, _to_read):
logging.debug("1 write_index %s , read_index %s , _to_read %s", self._write_index, self._read_index, _to_read)
if self._read_index > self._write_index:
_1st_read_size = len(self._mem_view) - self._read_index
_2nd_read_size = self._write_index
if _1st_read_size >= _to_read:
data = self._buffer[self._read_index: self._read_index + _to_read]
self._read_index += _to_read
else:
data = self._buffer[self._read_index:] + self._buffer[:_to_read - _1st_read_size]
self._read_index = _to_read - _1st_read_size
else: # write >= read
_read_less = min(_to_read, self._write_index - self._read_index)
# if self._write_index - self._read_index < _to_read:
# logging.debug("2 write_index %s , read_index %s , _to_read %s", self._write_index,self._read_index,_to_read)
# #return None
logging.debug("2 write_index %s , read_index %s , _to_read %s, _read %s", self._write_index, self._read_index, _to_read, _read_less)
data = self._buffer[self._read_index: self._read_index + _read_less]
self._read_index += _read_less
return data
I using it like:
self.__buffer = BufferHandling(self.__LOCALHOST_SOCKET_BUFFER_LEN * 4,
self.__LOCALHOST_SOCKET_BUFFER_LEN)
data = self.__buffer.read_from_buffer(_to_read)
if len(data) < _to_read:
self._selector.select(rlist = [self.__connection], elist = [self.__connection])
self.__buffer.fill_from_socket(self.__connection)
data += self.__buffer.read_from_buffer(_to_read)
After facing many issues, my question: is there a more Pythonic way to get the same affect?
or maybe suggest a more "clean" way to get some form of io buffering

I have created a short method to "clear" the buffer of a UDP socket. Here is the code:
def empty(sock):
"""Empty the UDP buffer."""
sock.setblocking(0)
while True:
try:
sock.recv(1024)
except BlockingIOError:
sock.setblocking(1)
return

Related

Crypto trading bot with Binance, error from callback <bound method (takes 2 positional arguments but 4 were given)

The bot takes certain data from www.testnet.binancefuture.com> the information I am getting are bid and ask, historical candles, contracts, balances, place, cancel and order status. The callback methods are on_open, on_error, on_close, on_message + Channel subscription. Here is the code and the error
class BinanceFuturesClient:
def __init__(self, public_key: str, secret_key: str, testnet: bool):
if testnet:
self._base_url = "https://testnet.binancefuture.com"
self._wss_url = "wss://stream.binancefuture.com/ws"
else:
self._base_url = "https://fapi.binance.com"
self._wss_url = "wss://fstream.binance.com/ws"
self._public_key = public_key
self._secret_key = secret_key
self._headers = {'X-MBX-APIKEY': self._public_key}
self.contracts = self.get_contracts()
self.balances = self.get_balances()
self.prices = dict()
self.strategies: typing.Dict[int, typing.Union[TechnicalStrategy, BreakoutStrategy]] = dict()
self.logs = []
self._ws_id = 1
self._ws = None
t = threading.Thread(target=self._start_ws)
t.start()
logger.info("Binance Futures Client successfully initialized")
def _add_log(self, msg: str):
logger.info("%s", msg)
self.logs.append({"log": msg, "displayed": False})
def _generate_signature(self, data: typing.Dict) -> str:
return hmac.new(self._secret_key.encode(), urlencode(data).encode(), hashlib.sha256).hexdigest()
def _make_request(self, method: str, endpoint: str, data: typing.Dict):
if method == "GET":
try:
response = requests.get(self._base_url + endpoint, params=data, headers=self._headers)
except Exception as e:
logger.error("Connection error while making %s request to %s: %s", method, endpoint, e)
return None
elif method == "POST":
try:
response = requests.post(self._base_url + endpoint, params=data, headers=self._headers)
except Exception as e:
logger.error("Connection error while making %s request to %s: %s", method, endpoint, e)
return None
elif method == "DELETE":
try:
response = requests.delete(self._base_url + endpoint, params=data, headers=self._headers)
except Exception as e:
logger.error("Connection error while making %s request to %s: %s", method, endpoint, e)
return None
else:
raise ValueError()
if response.status_code == 200:
return response.json()
else:
logger.error("Error while making %s request to %s: %s (error code %s)",
method, endpoint, response.json(), response.status_code)
return None
def get_contracts(self) -> typing.Dict[str, Contract]:
exchange_info = self._make_request("GET", "/fapi/v1/exchangeInfo", dict())
contracts = dict()
if exchange_info is not None:
for contract_data in exchange_info['symbols']:
if contract_data['marginAsset'] != "BUSD":
contracts[contract_data['symbol']] = Contract(contract_data, "binance")
return contracts
def get_historical_candles(self, contract: Contract, interval: str) -> typing.List[Candle]:
data = dict()
data['symbol'] = contract.symbol
data['interval'] = interval
data['limit'] = 1000
raw_candles = self._make_request("GET", "/fapi/v1/klines", data)
candles = []
if raw_candles is not None:
for c in raw_candles:
candles.append(Candle(c, interval, "binance"))
return candles
def get_bid_ask(self, contract: Contract) -> typing.Dict[str, float]:
data = dict()
data['symbol'] = contract.symbol
ob_data = self._make_request("GET", "/fapi/v1/ticker/bookTicker", data)
if ob_data is not None:
if contract.symbol not in self.prices:
self.prices[contract.symbol] = {'bid': float(ob_data['bidPrice']), 'ask': float(ob_data['askPrice'])}
else:
self.prices[contract.symbol]['bid'] = float(ob_data['bidPrice'])
self.prices[contract.symbol]['ask'] = float(ob_data['askPrice'])
return self.prices[contract.symbol]
def get_balances(self) -> typing.Dict[str, Balance]:
data = dict()
data['timestamp'] = int(time.time() * 1000)
data['signature'] = self._generate_signature(data)
balances = dict()
account_data = self._make_request("GET", "/fapi/v1/account", data)
if account_data is not None:
for a in account_data['assets']:
balances[a['asset']] = Balance(a, "binance")
return balances
def place_order(self, contract: Contract, order_type: str, quantity: float, side: str, price=None, tif=None) -> OrderStatus:
data = dict()
data['symbol'] = contract.symbol
data['side'] = side.upper()
data['quantity'] = round(round(quantity / contract.lot_size) * contract.lot_size, 8)
data['type'] = order_type
if price is not None:
data['price'] = round(round(price / contract.tick_size) * contract.tick_size, 8)
if tif is not None:
data['timeInForce'] = tif
data['timestamp'] = int(time.time() * 1000)
data['signature'] = self._generate_signature(data)
order_status = self._make_request("POST", "/fapi/v1/order", data)
if order_status is not None:
order_status = OrderStatus(order_status, "binance")
return order_status
def cancel_order(self, contract: Contract, order_id: int) -> OrderStatus:
data = dict()
data['orderId'] = order_id
data['symbol'] = contract.symbol
data['timestamp'] = int(time.time() * 1000)
data['signature'] = self._generate_signature(data)
order_status = self._make_request("DELETE", "/fapi/v1/order", data)
if order_status is not None:
order_status = OrderStatus(order_status, "binance")
return order_status
def get_order_status(self, contract: Contract, order_id: int) -> OrderStatus:
data = dict()
data['timestamp'] = int(time.time() * 1000)
data['symbol'] = contract.symbol
data['orderId'] = order_id
data['signature'] = self._generate_signature(data)
order_status = self._make_request("GET", "/fapi/v1/order", data)
if order_status is not None:
order_status = OrderStatus(order_status, "binance")
return order_status
def _start_ws(self):
self._ws = websocket.WebSocketApp(self._wss_url, on_open=self._on_open, on_close=self._on_close,
on_error=self._on_error, on_message=self._on_message)
while True:
try:
self._ws.run_forever()
except Exception as e:
logger.error("Binance error in run_forever() method: %s", e)
time.sleep(2)
def _on_open(self, ws):
logger.info("Binance connection opened")
self.subscribe_channel(list(self.contracts.values()), "bookTicker")
self.subscribe_channel(list(self.contracts.values()), "aggTrade")
def _on_close(self, ws):
logger.warning("Binance Websocket connection closed")
def _on_error(self, ws, msg: str):
logger.error("Binance connection error: %s", msg)
def _on_message(self, ws, msg: str):
data = json.loads(msg)
if "e" in data:
if data['e'] == "bookTicker":
symbol = data['s']
if symbol not in self.prices:
self.prices[symbol] = {'bid': float(data['b']), 'ask': float(data['a'])}
else:
self.prices[symbol]['bid'] = float(data['b'])
self.prices[symbol]['ask'] = float(data['a'])
# PNL Calculation
try:
for b_index, strat in self.strategies.items():
if strat.contract.symbol == symbol:
for trade in strat.trades:
if trade.status == "open" and trade.entry_price is not None:
if trade.side == "long":
trade.pnl = (self.prices[symbol]['bid'] - trade.entry_price) * trade.quantity
elif trade.side == "short":
trade.pnl = (trade.entry_price - self.prices[symbol]['ask']) * trade.quantity
except RuntimeError as e:
logger.error("Error while looping through the Binance strategies: %s", e)
if data['e'] == "aggTrade":
symbol = data['s']
for key, strat in self.strategies.items():
if strat.contract.symbol == symbol:
res = strat.parse_trades(float(data['p']), float(data['q']), data['T'])
strat.check_trade(res)
def subscribe_channel(self, contracts: typing.List[Contract], channel: str):
data = dict()
data['method'] = "SUBSCRIBE"
data['params'] = []
for contract in contracts:
data['params'].append(contract.symbol.lower() + "#" + channel)
data['id'] = self._ws_id
try:
self._ws.send(json.dumps(data))
except Exception as e:
logger.error("Websocket error while subscribing to %s %s updates: %s", len(contracts), channel, e)
self._ws_id += 1
def get_trade_size(self, contract: Contract, price: float, balance_pct: float):
balance = self.get_balances()
if balance is not None:
if 'USDT' in balance:
balance = balance['USDT'].wallet_balance
else:
return None
else:
return None
trade_size = (balance * balance_pct / 100) / price
trade_size = round(round(trade_size / contract.lot_size) * contract.lot_size, 8)
logger.info("Binance Futures current USDT balance = %s, trade size = %s", balance, trade_size)
return trade_size
The error after data:
error from callback <bound method BinanceFuturesClient._on_close of
<connectors.binance_futures.BinanceFuturesClient object at
0x000002B05C6B57C0>>: _on_close() takes 2 positional arguments but 4
were given
I did everything as in the "Udemy Cryptocurrency Trading Bot with a User Interface in Python" course. I can't figure out what's causing the error. I will be immensely grateful for any hints!
you've got to add the args and kwargs, it looks like this.
After you add them there'll be no errors
def _on_close(self, ws, *args, **kwargs):
logger.warning('Websocket connection closed')
self.ws_connected = False

What is the progress object to upload functions on Minio?

I know that this can be a dumb question but, what's the Progress object that I must pass to MinIO to see the upload status of an object?
This documentation:
https://docs.min.io/docs/python-client-api-reference.html
The method:
fput_object(bucket_name, object_name, file_path, content_type="application/octet-stream", metadata=None, sse=None, progress=None, part_size=0, num_parallel_uploads=3, tags=None, retention=None, legal_hold=False)
An example provided by the documentation:
# Upload data with progress bar.
result = client.fput_object(
"my-bucket", "my-object", "my-filename",
progress=Progress(),
)
print(
"created {0} object; etag: {1}, version-id: {2}".format(
result.object_name, result.etag, result.version_id,
),
)
The problem is the object Progress, I cannot find an applicable instance to run it. Can anyone help me on this or provide an example?
Just add the source code in the link.
https://github.com/minio/minio-py/blob/master/examples/progress.py
from minio import Minio
from minio.error import S3Error
import sys
import time
import urllib3
from queue import Empty, Queue
from threading import Thread
_BAR_SIZE = 20
_KILOBYTE = 1024
_FINISHED_BAR = '#'
_REMAINING_BAR = '-'
_UNKNOWN_SIZE = '?'
_STR_MEGABYTE = ' MB'
_HOURS_OF_ELAPSED = '%d:%02d:%02d'
_MINUTES_OF_ELAPSED = '%02d:%02d'
_RATE_FORMAT = '%5.2f'
_PERCENTAGE_FORMAT = '%3d%%'
_HUMANINZED_FORMAT = '%0.2f'
_DISPLAY_FORMAT = '|%s| %s/%s %s [elapsed: %s left: %s, %s MB/sec]'
_REFRESH_CHAR = '\r'
def main():
# Create client with anonymous access.
client = Minio(
"192.168.2.193:9000",
access_key="access",
secret_key="password",
secure=False,
http_client=urllib3.ProxyManager(
"http://192.168.2.193:9000",
timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
# cert_reqs="CERT_REQUIRED",
retries=urllib3.Retry(
total=5,
backoff_factor=0.2,
status_forcelist=[500, 502, 503, 504],
),
),
)
found = client.bucket_exists('test1')
if not found:
client.make_bucket('test1')
else:
print('Bucket test1 already exists')
client.fput_object('test1', 'dgk_csv_no_space.zip', 'D:/dgk_csv_no_space.zip', progress=Progress())
print("success")
class Progress(Thread):
"""
Constructs a :class:`Progress` object.
:param interval: Sets the time interval to be displayed on the screen.
:param stdout: Sets the standard output
:return: :class:`Progress` object
"""
def __init__(self, interval=1, stdout=sys.stdout):
Thread.__init__(self)
self.daemon = True
self.total_length = 0
self.interval = interval
self.object_name = None
self.last_printed_len = 0
self.current_size = 0
self.display_queue = Queue()
self.initial_time = time.time()
self.stdout = stdout
self.start()
def set_meta(self, total_length, object_name):
"""
Metadata settings for the object. This method called before uploading
object
:param total_length: Total length of object.
:param object_name: Object name to be showed.
"""
self.total_length = total_length
self.object_name = object_name
self.prefix = self.object_name + ': ' if self.object_name else ''
def run(self):
displayed_time = 0
while True:
try:
# display every interval secs
task = self.display_queue.get(timeout=self.interval)
except Empty:
elapsed_time = time.time() - self.initial_time
if elapsed_time > displayed_time:
displayed_time = elapsed_time
self.print_status(current_size=self.current_size,
total_length=self.total_length,
displayed_time=displayed_time,
prefix=self.prefix)
continue
current_size, total_length = task
displayed_time = time.time() - self.initial_time
self.print_status(current_size=current_size,
total_length=total_length,
displayed_time=displayed_time,
prefix=self.prefix)
self.display_queue.task_done()
if current_size == total_length:
self.done_progress()
def update(self, size):
"""
Update object size to be showed. This method called while uploading
:param size: Object size to be showed. The object size should be in
bytes.
"""
if not isinstance(size, int):
raise ValueError('{} type can not be displayed. '
'Please change it to Int.'.format(type(size)))
self.current_size += size
self.display_queue.put((self.current_size, self.total_length))
def done_progress(self):
self.total_length = 0
self.object_name = None
self.last_printed_len = 0
self.current_size = 0
def print_status(self, current_size, total_length, displayed_time, prefix):
formatted_str = prefix + format_string(
current_size, total_length, displayed_time)
self.stdout.write(_REFRESH_CHAR + formatted_str + ' ' *
max(self.last_printed_len - len(formatted_str), 0))
self.stdout.flush()
self.last_printed_len = len(formatted_str)
def seconds_to_time(seconds):
"""
Consistent time format to be displayed on the elapsed time in screen.
:param seconds: seconds
"""
minutes, seconds = divmod(int(seconds), 60)
hours, m = divmod(minutes, 60)
if hours:
return _HOURS_OF_ELAPSED % (hours, m, seconds)
else:
return _MINUTES_OF_ELAPSED % (m, seconds)
def format_string(current_size, total_length, elapsed_time):
"""
Consistent format to be displayed on the screen.
:param current_size: Number of finished object size
:param total_length: Total object size
:param elapsed_time: number of seconds passed since start
"""
n_to_mb = current_size / _KILOBYTE / _KILOBYTE
elapsed_str = seconds_to_time(elapsed_time)
rate = _RATE_FORMAT % (
n_to_mb / elapsed_time) if elapsed_time else _UNKNOWN_SIZE
frac = float(current_size) / total_length
bar_length = int(frac * _BAR_SIZE)
bar = (_FINISHED_BAR * bar_length +
_REMAINING_BAR * (_BAR_SIZE - bar_length))
percentage = _PERCENTAGE_FORMAT % (frac * 100)
left_str = (
seconds_to_time(
elapsed_time / current_size * (total_length - current_size))
if current_size else _UNKNOWN_SIZE)
humanized_total = _HUMANINZED_FORMAT % (
total_length / _KILOBYTE / _KILOBYTE) + _STR_MEGABYTE
humanized_n = _HUMANINZED_FORMAT % n_to_mb + _STR_MEGABYTE
return _DISPLAY_FORMAT % (bar, humanized_n, humanized_total, percentage,
elapsed_str, left_str, rate)
if __name__ == '__main__':
try:
main()
except S3Error as exc:
print("error ouccured.", exc)

Python Multi-Threading, append different values to a method

See the code below, "J2".
In the xml j2 as you can see "Y=getY" it's doing that for each id+pproxy(multithreading)
Now what I want is, I want it to continue changing id and k2= but use the same Y for only 3 times. Then after that get a new Y and do the same..
So for example
<j2 Y="one value" id="3 multi threads pass trough" k="3 multi threads"
and then after that <j2 Y="new y value because only 3 ids can pass through one Y" id="same again" k="same again">
j2 = str('<j2 cb="'+rcv.attrib["c"]+'" Y="'+str(Y[0])+'" l5="'+str(Y[1]).strip()+'" l4="583" l3="463" l2="0" q="1" y="'+rcv.attrib['i']+'" k="'+k+'" k3="0" p="0" c="'+str(info[2])+'" f="0" u="'+str(uid)+'" d0="0" n=" " a="0" h="" v="0" />\0')
The Y value should be the same while 3 ids pass through it, then when 3 have passed through it, grab a new Y value.
Take a look at this script:
import socket, socks
import xml.etree.ElementTree as tree
import time, random, urllib2
import threading
from itertools import izip
def gB(data,first,last):
x=len(first)
begin = data.find(first) + x
end = data.find(last,begin)
return data[begin:end]
def isInt(buf):
try:
i = int(buf)
return True
except:
return False
def ip(roomname):
if isInt(roomname):
room = roomname
else:
room = gB(urllib2.urlopen('http://xat.com/'+roomname).read(),'FlashVars="id=','&xc=')
room = int(room)
x = urllib2.urlopen('http://xat.com/web_gear/chat/ip.htm?'+str(time.time())).read()
x = tree.fromstring(x)
xSock = x.attrib['xSock'].split(',')
rSock = []
while len(rSock) < 4:
rSock.append(xSock[len(rSock) * 4 + random.randint(0,1)])
return rSock[(room & 96) >> 5],int(10007 + (room % 32)),room
def parseID(auser3):
auser3 = auser3.split('&')
userid = auser3[1].split('=')[1]
k1 = auser3[2].split('=')[1]
k2 = auser3[3].split('=')[1]
return [userid,k1,k2]
return l5
def getY(au, p0, p1, p2, p3, yi):
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(("127.0.0.1",1337))
sock.send(au+"_"+p0+"_"+p1+"_"+p2+"_"+p3+"_"+yi)
data = sock.recv(1270)
parse = data.split("\"")
sock.close()
print "Y => "+parse[1]+" L5 => "+parse[3]
return [parse[1], parse[3]]
def raider(a3,p):
info = ip(chat)
try:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, p[0], int(p[1]))
socket.test = socks.socksocket
xat = socket.test(socket.AF_INET, socket.SOCK_STREAM, socket.SOL_TCP)
xat.settimeout(10)
xat.connect((info[0],int(info[1])))
xat.send('<y r="'+str(info[2])+'" />\0')
bypass = xat.recv(1024)
print "\nRecv --> "+bypass+"\n"
rcv = tree.fromstring(bypass.strip("\0"))
if 'i' not in rcv.attrib:
raise Exception("YUP")
import pprint
pprint.pprint(a3)
uid = a3[0]
k = a3[1]
if 'au' in rcv.attrib:
Y = getY(rcv.attrib["au"], rcv.attrib["p"].split("_")[0],rcv.attrib["p"].split("_")[1],rcv.attrib["p"].split("_")[2],rcv.attrib["p"].split("_")[3], rcv.attrib["i"])
j2 = str('<j2 cb="'+rcv.attrib["c"]+'" Y="'+str(Y[0])+'" l5="'+str(Y[1]).strip()+'" l4="583" l3="463" l2="0" q="1" y="'+rcv.attrib['i']+'" k="'+k+'" k3="0" p="0" c="'+str(info[2])+'" f="0" u="'+str(uid)+'" d0="0" n=" " a="0" h="" v="0" />\0')
xat.send(j2)
print "\nSend [Bypass] --> "+j2+"\n"
else:
Y = getY(str(0), rcv.attrib["p"].split("_")[0],rcv.attrib["p"].split("_")[1],rcv.attrib["p"].split("_")[2],rcv.attrib["p"].split("_")[3], rcv.attrib["i"])
j2 = str('<j2 cb="'+rcv.attrib["c"]+'" l5="'+str(Y[1]).strip()+'" l4="583" l3="463" l2="0" q="1" y="'+rcv.attrib['i']+'" k="'+k+'" k3="0" p="0" c="'+str(info[2])+'" f="0" u="'+str(uid)+'" d0="0" n=" " a="0" h="" v="0" />\0')
xat.send(j2)
print "\nSend --> "+j2+"\n"
while 1:
time.sleep(1)
xat.send('<m t=" | XAT IS CORRUPT | " u="'+uid+'" />\0')
except:
pass
chat = raw_input("Chat to raid: ")
ids = [i.strip() for i in open('ids.txt','r')]
proxies = [i.strip() for i in open('socks.txt','r')]
for (i,j) in izip(ids,proxies):
i = parseID(i)
j = j.split(':')
threading.Thread(target=raider,args=(i,j)).start()

ip address networks in Python 3.3

I am trying to convert a group of IP ranges that can start and end on any given address, but not necessarily on a .0, .127, or .255, etc. I have code that is mostly working; however, it can be slow for large ranges.
For example find_range("1.40.0.0","1.44.255.255") will take over a minute to return the correct result of 1.40.0.0/14 and 1.44.0.0/16.
Also, I am have trouble when the starting range does not end with a .0. How can I fix these 2 issues: slowness on large IP ranges and when the starting range does not end in .0?
For the slowness problem, I tried skipping more than 1 address at a time, but then this would miss smaller ranges.
import ipaddress, socket, struct
def ip2int(addr):
return struct.unpack("!I", socket.inet_aton(addr))[0]
def int2ip(addr):
return socket.inet_ntoa(struct.pack("!I", addr))
def ipminus(ip, amount=1):
tmp = ip2int(ip)
return int2ip(tmp - amount)
def ipplus(ip):
tmp = ip2int(ip)
return int2ip(tmp + 1)
def cidr_notation(a,b):
for mask in range(32, 6, -1):
test = "%s/%s" % (a,mask)
try:
n = ipaddress.IPv4Network(test,False)
if b == "%s" % (n.broadcast_address):
return test
except:
pass
return None
def split_range(a,b):
a1 = ip2int(a)
b1 = ip2int(b)
needed = 1
while needed:
result = cidr_notation(a,b)
if result:
print( "* %16s\t%16s\t%16s" % (result, a, b))
if ip2int(b) > b1:
needed = 0
else:
a = ipplus(b)
b = int2ip(b1)
else:
b = ipminus(b)
return result
def find_range(x,y):
result = cidr_notation(x,y)
if result:
print( "# %16s\t%16s\t%16s" % (result, x, y))
else:
split_range(x,y)
# main...
print("%16s\t%16s\t%16s" % ("mask","start","end"))
print("%16s\t%16s\t%16s" % ("----","-----","---"))
find_range("128.191.0.0","128.191.255.255") #fast
find_range("10.55.96.106","10.55.96.106") #fast
find_range("5.135.14.0","5.135.61.11") #slow
find_range("4.31.64.72","4.59.175.255") #does not work, how to fix?
find_range("1.40.0.0","1.44.255.255") #very slow
# 5000 more find_range() calls...
Based on bsdlp's comment, this code got a lot simpler and faster!
def find_range(x,y,c_code="",c_name=""):
print()
print("%29s\t%23s" % (x,y))
print("%16s\t%16s\t%16s\t%4s\t%s" % ("mask","start","end","code","name"))
print("%16s\t%16s\t%16s\t%4s\t%s" % ("----","-----","---","----","----"))
result = ipaddress.summarize_address_range( ipaddress.IPv4Address(x), ipaddress.IPv4Address(y) )
for entry in result:
net = str( entry.network_address )
bcast = str( entry.broadcast_address )
print( "%16s\t%16s\t%16s\t%4s\t%s" % (entry, net, bcast,c_code,c_name))

Reference to value of the function

At beginning i wanna say i'm newbie in use Python and everything I learned it came from tutorials.
My problem concerning reference to the value. I'm writing some script which is scrapping some information from web sites. I defined some function:
def MatchPattern(count):
sock = urllib.urlopen(Link+str(count))
htmlSource = sock.read()
sock.close()
root = etree.HTML(htmlSource)
root = etree.HTML(htmlSource)
result = etree.tostring(root, pretty_print=True, method="html")
expr1 = check_reg(root)
expr2 = check_practice(root)
D_expr1 = no_ks(root)
D_expr2 = Registred_by(root)
D_expr3 = Name_doctor(root)
D_expr4 = Registration_no(root)
D_expr5 = PWZL(root)
D_expr6 = NIP(root)
D_expr7 = Spec(root)
D_expr8 = Start_date(root)
#-----Reg_practice-----
R_expr1 = Name_of_practise(root)
R_expr2 = TERYT(root)
R_expr3 = Street(root)
R_expr4 = House_no(root)
R_expr5 = Flat_no(root)
R_expr6 = Post_code(root)
R_expr7 = City(root)
R_expr8 = Practice_no(root)
R_expr9 = Kind_of_practice(root)
#------Serv_practice -----
S_expr1 = TERYT2(root)
S_expr2 = Street2(root)
S_expr3 = House_no2(root)
S_expr4 = Flat_no2(root)
S_expr5 = Post_code2(root)
S_expr6 = City2(root)
S_expr7 = Phone_no(root)
return expr1
return expr2
return D_expr1
return D_expr2
return D_expr3
return D_expr4
return D_expr5
return D_expr6
return D_expr7
return D_expr8
#-----Reg_practice-----
return R_expr1
return R_expr2
return R_expr3
return R_expr4
return R_expr5
return R_expr6
return R_expr7
return R_expr8
return R_expr9
#------Serv_practice -----
return S_expr1
return S_expr2
return S_expr3
return S_expr4
return S_expr5
return S_expr6
return S_expr7
So now inside the script I wanna check value of the expr1 returned by my fynction. I don't know how to do that. Can u guys help me ? Is my function written correct ?
EDIT:
I can't add answer so I edit my current post
This is my all script. Some comments are in my native language but i add some in english
#! /usr/bin/env python
#encoding:UTF-8-
# ----------------------------- importujemy potrzebne biblioteki i skrypty -----------------------
# ------------------------------------------------------------------------------------------------
import urllib
from lxml import etree, html
import sys
import re
import MySQLdb as mdb
from TOR_connections import *
from XPathSelection import *
import os
# ------------------------------ Definiuje xPathSelectors ------------------------------------------
# --------------------------------------------------------------------------------------------------
# -------Doctors -----
check_reg = etree.XPath("string(//html/body/div/table[1]/tr[3]/td[2]/text())") #warunek Lekarz
check_practice = etree.XPath("string(//html/body/div/table[3]/tr[4]/td[2]/text())") #warunek praktyka
no_ks = etree.XPath("string(//html/body/div/table[1]/tr[1]/td[2]/text())")
Registred_by = etree.XPath("string(//html/body/div/table[1]/tr[4]/td[2]/text())")
Name_doctor = etree.XPath("string(//html/body/div/table[2]/tr[2]/td[2]/text())")
Registration_no = etree.XPath("string(//html/body/div/table[2]/tr[3]/td[2]/text())")
PWZL = etree.XPath("string(//html/body/div/table[2]/tr[4]/td[2]/text())")
NIP = etree.XPath("string(//html/body/div/table[2]/tr[5]/td[2]/text())")
Spec = etree.XPath("string(//html/body/div/table[2]/tr[18]/td[2]/text())")
Start_date = etree.XPath("string(//html/body/div/table[2]/tr[20]/td[2]/text())")
#-----Reg_practice-----
Name_of_practise = etree.XPath("string(//html/body/div/table[2]/tr[1]/td[2]/text())")
TERYT = etree.XPath("string(//html/body/div/table[2]/tr[7]/td[2]/*/text())")
Street = etree.XPath("string(//html/body/div/table[2]/tr[8]/td[2]/text())")
House_no = etree.XPath("string(//html/body/div/table[2]/tr[9]/td[2]/*/text())")
Flat_no = etree.XPath("string(//html/body/div/table[2]/tr[10]/td[2]/*/text())")
Post_code = etree.XPath("string(//html/body/div/table[2]/tr[11]/td[2]/*/text())")
City = etree.XPath("string(//html/body/div/table[2]/tr[12]/td[2]/*/text())")
Practice_no = etree.XPath("string(//html/body/div/table[3]/tr[4]/td[2]/text())")
Kind_of_practice = etree.XPath("string(//html/body/div/table[3]/tr[5]/td[2]/text())")
#------Serv_practice -----
TERYT2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[2]/td[2]/*/text())")
Street2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[3]/td[2]/text())")
House_no2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[4]/td[2]/*/text())")
Flat_no2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[5]/td[2]/i/text())")
Post_code2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[6]/td[2]/*/text())")
City2 = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[7]/td[2]/*/text())")
Phone_no = etree.XPath("string(//html/body/div/table[3]/tr[14]/td/table/tr[8]/td[2]/text())")
# --------------------------- deklaracje zmiennych globalnych ----------------------------------
# ----------------------------------------------------------------------------------------------
decrease = 9
No = 1
Link = "http://rpwdl.csioz.gov.pl/rpz/druk/wyswietlKsiegaServletPub?idKsiega="
# --------------------------- funkcje zdefiniowane ----------------------------------
# ----------------------------------------------------------------------------------------------
def MatchPattern(count):
sock = urllib.urlopen(Link+str(count))
htmlSource = sock.read()
sock.close()
root = etree.HTML(htmlSource)
root = etree.HTML(htmlSource)
result = etree.tostring(root, pretty_print=True, method="html")
expr1 = check_reg(root)
expr2 = check_practice(root)
D_expr1 = no_ks(root)
D_expr2 = Registred_by(root)
D_expr3 = Name_doctor(root)
D_expr4 = Registration_no(root)
D_expr5 = PWZL(root)
D_expr6 = NIP(root)
D_expr7 = Spec(root)
D_expr8 = Start_date(root)
#-----Reg_practice-----
R_expr1 = Name_of_practise(root)
R_expr2 = TERYT(root)
R_expr3 = Street(root)
R_expr4 = House_no(root)
R_expr5 = Flat_no(root)
R_expr6 = Post_code(root)
R_expr7 = City(root)
R_expr8 = Practice_no(root)
R_expr9 = Kind_of_practice(root)
#------Serv_practice -----
S_expr1 = TERYT2(root)
S_expr2 = Street2(root)
S_expr3 = House_no2(root)
S_expr4 = Flat_no2(root)
S_expr5 = Post_code2(root)
S_expr6 = City2(root)
S_expr7 = Phone_no(root)
return expr1
return expr2
return D_expr1
return D_expr2
return D_expr3
return D_expr4
return D_expr5
return D_expr6
return D_expr7
return D_expr8
#-----Reg_practice-----
return R_expr1
return R_expr2
return R_expr3
return R_expr4
return R_expr5
return R_expr6
return R_expr7
return R_expr8
return R_expr9
#------Serv_practice -----
return S_expr1
return S_expr2
return S_expr3
return S_expr4
return S_expr5
return S_expr6
return S_expr7
# --------------------------- ustanawiamy polaczenie z baza danych -----------------------------
# ----------------------------------------------------------------------------------------------
con = mdb.connect('localhost', 'root', '******', 'SANBROKER', charset='utf8');
# ---------------------------- początek programu -----------------------------------------------
# ----------------------------------------------------------------------------------------------
with con:
cur = con.cursor()
cur.execute("SELECT Old_num FROM SANBROKER.Number_of_records;")
Old_num = cur.fetchone()
count = Old_num[0]
counter = input("Input number of rows: ")
# ----------------------- pierwsze połączenie z TORem ------------------------------------
# ----------------------------------------------------------------------------------------
#connectTor()
#conn = httplib.HTTPConnection("my-ip.heroku.com")
#conn.request("GET", "/")
#response = conn.getresponse()
#print(response.read())
while count <= counter: # co dziesiata liczba
# --------------- pierwsze wpisanie do bazy danych do Archive --------------------
with con:
cur = con.cursor()
cur.execute("UPDATE SANBROKER.Number_of_records SET Archive_num=%s",(count))
# ---------------------------------------------------------------------------------
if decrease == 0:
MatchPattern(count)
# Now I wanna check some expresions (2 or 3)
# After that i wanna write all the values into my database
#------- ostatnie czynności:
percentage = count / 100
print "rekordów: " + str(count) + " z: " + str(counter) + " procent dodanych: " + str(percentage) + "%"
with con:
cur = con.cursor()
cur.execute("UPDATE SANBROKER.Number_of_records SET Old_num=%s",(count))
decrease = 10-1
count +=1
else:
MatchPattern(count)
# Now I wanna check some expresions (2 or 3)
# After that i wanna write all the values into my database
# ------ ostatnie czynności:
percentage = count / 100
print "rekordów: " + str(count) + " z: " + str(counter) + " procent dodanych: " + str(percentage) + "%"
with con:
cur = con.cursor()
cur.execute("UPDATE SANBROKER.Number_of_records SET Old_num=%s",(count))
decrease -=1
count +=1
Well, I'm assuming check_reg is a function that returns a boolean (either True or False).
If that's the case, to check the return:
if expr1:
print "True."
else:
print "False"
There's more than one way to do it, but basically, if expr1: is all you need to do the checking.
To capture the return value of a function, assign the function to a name with an equal sign, like this:
return_value = somefunction(some_value)
print('The return value is ',return_value)
Keep in mind that when the first return statement is encountered, the function will exit. So if you have more than one return statement after each other, only the first will execute.
If you want to return multiple things, add them to a list and then return the list.
Here is an improved version of your function:
def match_pattern(count):
sock = urllib.urlopen(Link+str(count))
htmlsource = sock.read()
sock.close()
root = etree.HTML(htmlSource)
# root = etree.HTML(htmlSource) - duplicate line
# result = etree.tostring(root, pretty_print=True, method="html")
function_names = [check_reg, check_practice, no_ks, Registered_by, \
Name_doctor, Registration_no, PWZL, NIP, Spec, Start_date, \
Name_of_practise, TERYT, Street, House_no2, Flat_no, \
Post_code2, City2, Phone_no]
results = []
for function in function_names:
results.append(function(root))
return results
r = match_pattern(1)
print r[0] # this will be the result of check_reg(root)
The code you have posted is quite ambigous. Can you please fix the ident to let us know what belongs to the function and which part is the script.
A function can returns only one value. You cannot do :
return something
return something_else
return ...
The function will ends when first value will be returned.
What you can do is returning a list, tuple or dict containing all your values.
For instance :
return (something,something_else,...)
or
return [something,something_else,...]
In your case, it seems better to create a class that would have all values you want as attributes, and turn this function into a method that would set the attributes values.
class Example(object):
def __init__ ( self , link , count ):
sock = urllib.urlopen(link+str(count))
htmlSource = sock.read()
sock.close()
root = etree.HTML(htmlSource)
root = etree.HTML(htmlSource)
result = etree.tostring(root, pretty_print=True, method="html")
self.expr1 = check_reg(root)
self.expr2 = check_practice(root)
self.D_expr1 = no_ks(root)
...
self.D_expr8 = Start_date(root)
#-----Reg_practice-----
self.R_expr1 = Name_of_practise(root)
...
self.R_expr9 = Kind_of_practice(root)
#------Serv_practice -----
self.S_expr1 = TERYT2(root)
...
self.S_expr7 = Phone_no(root)
Then you will be able to use this class like :
exampleInstance = Example ( "link you want to use" , 4 ) # the second argument is your 'count' value
# Now you can use attributes of your class to get the values you want
print exampleInstance . expr1
print exampleInstance . S_expr7

Categories