I’ve been trying to find a proper solution to my problem for several days now looking everywhere. Hopefully some of you guys can direct me to the right direction.
I am trying to implement a system with proxy list. Where system is picking up proxy from list, trying to connect using it and if it failed to connect, modify the line of the proxy in proxy.txt with ” = 1″ means Timed Out one time.
When system will stumble on this proxy again next time and it will fail again, it should modify ” = 1″ to ” = 2″ etc. Converting string to int, changing values and then converting back to string is not difficult to do.
My problem here is what I can’t find the way how to put cursor to specific proxy and then replace ” = Number” value. I can find proxy by using .seek(), but taking into account that every proxy has different length of characters .seek(proxy + 20) simply won’t work…
For example, proxy.txt has:
192.168.0.1:8000 = 2
192.168.0.10:80 = 1
192.168.0.100:3128 = 2
192.168.0.4:8080
192.168.0.5:7822 = 2
192.168.0.6:8005
Even if I can find the proxy I need by scanning every line and then fire .seek(address of the proxy), how can I move then to “=” character?
I know if might be much easier just to copy everything from text file to pickle file and then modify everything there by using dictionaries etc.
But the idea is that I can open text file at any time and see what proxies are failing.
Thanks.
Depending on the file size, and whether you will be changing the length of an entry in the file, it is probably easier to just read the entire file into memory. However, if you still want to read/write in place:
Say I want to modify the entry
192.168.0.10:80 = 1so it reads192.168.0.10:80 = 2.NOTE: This will only work if you are not modifying the length of any of the entries (including adding whitespace) if the length of the entries changes at all (i.e. if you are changing
192.168.0.10:80\nto192.168.0.10:80 = 1\n) then you will overwrite a portion of the next line in the file. If this is the case, you are better off reading the file into memory and then modifying it.