I'm trying to use a variable defined in one function in a different function. I know I have to use Global var but I'm still not getting the output I was expecting.
def proxycall():
global ip
global port
ip = "192.168.0.1"
port = "8080"
def web1():
class YWebPage():
def something():
var = 1
def somethingelse():
varr = 2
class Browser():
def someting():
varrr = 3
def sometingelse():
varrrr = 4
print (ip)
print (port)
web1()
This minimal version of my program is giving the traceback - NameError: global name 'ip' is not defined.
I see the problem with the minimal version now, thanks for pointing it out. However I don't think thats my issue now because I call proxycall() in the full version below, any ideas?
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebSettings
from PyQt4.QtNetwork import QNetworkAccessManager
from PyQt4.QtNetwork import *
import re
import requests
UA_STRING = """Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36""" # String for User-Agent
#vidurl = "http://www.youtube.com/watch?v=hqDT6G5_1tQ" # 94 views
vidurl = "http://httpbin.org"
def proxycall():
global ip
global port
# Proxy Stuff
count = 0
while (count < 5):
#Call mashape proxy API and setup
try:
headers = {'X-Mashape-Authorization':'xxxxxxxxxxxxx'}
p = requests.get('https://webknox-proxies.p.mashape.com/proxies/new?maxResponseTime=5', headers=headers, timeout=5.000)
prox = p.text
ip = prox.split(":")[1] #split response string
port = prox.split(":")[2]
ip = re.sub('["}]', '', ip) #sanitize output
port = re.sub('["}]', '', port)
proxy = str(ip) + ':' + str(port) #reformat proxy for Requests
print ('Retrieved proxy ' + str(proxy))
proxies = {"http": proxy}
print ('Configured proxy')
except:
print('Mashape API error, FAIL')
#Get real IP address
try:
r = requests.get('http://httpbin.org/ip', timeout=5.000)
s = r.text
realip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', s )
print ('Real IP address is ' + str(realip))
except:
print ("Connection Error (IP), FAIL")
#Test proxy
try:
x = requests.get('http://bot.whatismyipaddress.com/', proxies=proxies, timeout=5.000)
y = x.text
proxyip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', y )
print ('Masked IP address is ' + str(proxyip))
if proxyip != realip:
print ('Proxy test OK')
count = 100
else:
print ('Proxy test FAIL, GET new proxy...')
except:
print ("Connection Error (Proxy), FAIL")
def web1(parent):
class YWebPage(QtWebKit.QWebPage):
def __init__(self):
super(QtWebKit.QWebPage, self).__init__()
def userAgentForUrl(self, url):
return UA_STRING
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
self.resize(800,600) # Viewport size
self.centralwidget = QtGui.QWidget(self)
self.html = QtWebKit.QWebView()
def browse(self):
self.webView = QtWebKit.QWebView()
self.yPage = YWebPage()
self.webView.setPage(self.yPage)
self.webView.load(QtCore.QUrl(vidurl)) # Video URL
self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) # Enables flash player
self.webView.settings().setAttribute(QtWebKit.QWebSettings.AutoLoadImages, False) # No images for speed
QWebSettings.clearMemoryCaches ()
self.webView.show()
proxycall()
x = Browser(parent)
QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, ip, port))
x.browse()
You didn't call proxycall() at all, so the variables were never set.
Call it first, then call web1():
proxycall()
web1()
Names don't just pop into existence just because you marked them as global in a function; Python names require assignment (binding) to materialize.
You need to call proxycall() before you can access ip globally:
proxycall()
web1()
Your updated code has the same problem as the original code. You are using ip in this statement:
QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, ip, port))
...but you don't call proxycall until you call x.browse() in the statement after that.
You need to call proxycall() to initialize ip
proxycall()
web1()
You need to initialize/define ip and port before using them. Call proxycall() and then web1()
def proxycall():
global ip
global port
ip = "192.168.0.1"
port = "8080"
def web1():
class YWebPage():
def something():
var = 1
def somethingelse():
varr = 2
class Browser():
def someting():
varrr = 3
def sometingelse():
varrrr = 4
print (ip)
print (port)
proxycall()
web1()
ans:
192.168.0.1
8080
Related
I'm trying to pull the query string from a url typed in by the user which would be turned into an interger, so http://127.0.0.1:8000/?1653. When i try to do this python gives me the error,
File "C:\Users...\Documents\webserver\server.py", line 26, in do_GET
int(URIextra) ValueError: invalid literal for int() with base 10: ''
I don't know what i'm doing wrong here, any help would be much appreciated.
import http.server, socketserver, os
from urllib.parse import urlparse
from shutil import copyfile
dirs = []
PORT = 8000
URIextra = ""
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, req, client_addr, server):
http.server.SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)
def do_GET(self):
o = urlparse(self.path)
URIextra = str(o[4])
http.server.SimpleHTTPRequestHandler.do_GET(self)
print(URIextra)
dirs = os.listdir()
f = open("index.txt", "w")
f.write(os.listdir())
f.close()
int(URIextra)
copyfile(dirs[URIextra], "CurrentFile.mp3")
class MyTCPServer(socketserver.ThreadingTCPServer):
allow_reuse_address = True
os.chdir(os.getcwd() + "/web")
httpd = MyTCPServer(('localhost', PORT), CustomHandler)
httpd.allow_reuse_address = True
print("Serving at port", PORT)
httpd.serve_forever()
You are using URIExtra as a string:
URIextra = str(o[4])
you should use this:
URIextra = int(o[4])
I was able to change my code to work more efficiently, don't have my PC with me right now, will post it here in a bit
I have written a script to perform telnet, which i wanted to use for sending commands to my Device Under Test (router).
My Telnet Script:
import sys, time, telnetlib
sys.path.insert(0, '/tmp')
import options
def telnet_connect():
HOST = "%s" %options.DUT_telnet_ip
PORT = "%s" %options.DUT_telnet_port
username = "%s" %options.DUT_telnet_username
password = "%s" %options.DUT_telnet_password
tn = telnetlib.Telnet(HOST, PORT, 10)
time.sleep(5)
tn.write("\n")
tn.read_until("login:", 2)
tn.write(username)
tn.read_until("Password:", 2)
tn.write(password)
tn.write("\n")
response = tn.read_until("$", 5)
return response
def telnet_close():
response = tn.write("exit\n")
return response
I want to use this script in another program which will check the version of the Router by telneting it. I expect a script which will call my above function to perform telnet and send other commands viz. "version" or "ls"
Try to make this more like a class:
import sys, time, telnetlib
sys.path.insert(0, '/tmp')
class TelnetConnection():
def init(self, HOST, PORT):
self.tn = telnetlib.Telnet(HOST, PORT, 10)
def connect(self, username, password):
tn = self.tn
tn.write("\n")
tn.read_until("login:", 2)
tn.write(username)
tn.read_until("Password:", 2)
tn.write(password)
tn.write("\n")
response = tn.read_until("$", 5)
return response
def close(self):
tn = self.tn
response = tn.write("exit\n")
return response
# create here then a method to communicate as you wish
Then you can use it as follows:
import options
HOST = "%s" %options.DUT_telnet_ip
PORT = "%s" %options.DUT_telnet_port
username = "%s" %options.DUT_telnet_username
password = "%s" %options.DUT_telnet_password
connection = TelnetConnection(HOST, PORT)
connection.connect(username, password)
connection.do_all_operations_you_want() # write your own method for that
connection.close()
I've been trying to make a proxy server that allows UA spoofing but, it doesn't seem to work and I don't know why.
config.py
# if you want to spoof the useragent then,
# replace "$MyAgent" with the desired agent.
# For example,
# agent="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:44.0) Gecko/20100101 Firefox/44.0"
host="127.0.0.1"
port=8888
byte=4096
listeners=100
delay=00000.1
agent="$MyAgent"
Proxy.py
import socket, select, time, config, sys
def Print(val):
sys.stdin.flush()
sys.stdout.write(val)
class ProxyServer(object):
def __init__(self):
self._host = (config.host, config.port)
self._socket = socket.socket()
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._socket.setblocking(False)
self._agent = config.agent
self._bytes = config.byte
self._delay = config.delay
self._listeners = config.listeners
self._cons = list()
self._log = Print
def _bind(self):
self._socket.bind(self._host)
self._socket.listen(self._listeners)
def main(self):
self._cons.append(self._socket)
while True:
time.sleep(self._delay)
rl, xl, wl = select.select(self._cons, [], [])
for sock in rl:
if sock == self._socket:
sock, ip = self._socket.accept()
self._on_connect()
self._cons.append(sock)
elif sock == None:
self._socket.close()
data = sock.recv(self._bytes)
agent = self._get_agent_header(data)
if not agent == "NO_AGENT":
agent_new = self._agent.replace("$MyAgent", agent)
data = data.replace(agent, agent_new)
sock.send(data)
else:
sock.send(data)
if not data:
self._on_close(sock)
def _on_close(self, sock):
self._cons.remove(sock)
self._log("client dc {0} left".format(self._count()))
sock.close()
def _on_connect(self):
self._log("connection made, {0} clients connected".format(self._count()))
def _count(self):
c = len(self._cons) - 1
return c
def _get_agent_header(self, data):
pat = "User-Agent: (.+)"
m = re.search(pat, data)
if m:
return m.group(0)
else:
return "NO_AGENT"
if __name__ == '__main__':
server = ProxyServer()
server._bind()
server.main()
If you run this the request will not complete. But, some of the headers will show in plain text. Is there something I'm not doing right?
How can you load test gevent sockets?
I've written a simple server and I want to load test this before I tweak the code to find out what improvements can be done.
Are there any specifics to load testing a socket server?
How would I go about load testing this code so I can compare the results after applying tweaks?
from gevent import server
from gevent.monkey import patch_all; patch_all()
import gevent
class Client(object):
def __init__(self, socket, address, server_handler):
self.socket = socket
self.address = address
self.server_handler = server_handler
gevent.spawn(self.listen)
def listen(self):
f = self.socket.makefile()
while True:
line = f.readline()
if not line:
print 'client died'
break
line = line.rstrip()
for addr, c in self.server_handler.users.iteritems():
msg = '<%s> says: %s' % (self.address, line)
c.send(msg)
print '<%s>: %s' % (self.address, line)
def send(self, msg):
f = self.socket.makefile()
f.write('%s\r\n' % msg)
f.flush()
class ServerHandler(object):
def __init__(self):
self.users = {}
def __call__(self, socket, address):
client = Client(socket, address, self)
self.users[address] = client
self.socket = socket
self.address = address
print 'connection made'
if __name__ == '__main__':
server = server.StreamServer(('0.0.0.0', 5000), ServerHandler())
server.serve_forever()
I've got a subclass of QTcpSocket. And problem is : when i firt time connect to server - everything ok, but after socket connected i restart server (python socketServer,just close and start script again) socket disconnecting and tryin to reconnect while server is down, but when i start server again - nothing happened, socket.state() always in ConnectingState.. what is wrong ?
Here example code:
# -*- coding: utf-8 -*-
from PyQt4.QtCore import QVariant, QTimer, pyqtSignal, QCoreApplication
import sys
from PyQt4.QtNetwork import QTcpSocket
from re import match
import json
MAX_WAIT_LEN = 8
class UpQSocket(QTcpSocket):
data_ready = pyqtSignal(unicode)
def __init__(self):
QTcpSocket.__init__(self)
self.wait_len = ''
self.temp = ''
self.setSocketOption(QTcpSocket.KeepAliveOption, QVariant(1))
self.readyRead.connect(self.on_ready_read)
self.connected.connect(self.on_connected)
self.disconnected.connect(self.on_disconnect)
self.error.connect(self.on_error)
self.data_ready.connect(self.print_command)
def connectToHost(self, host, port):
print 'connectToHost'
self.temp = ''
self.wait_len = ''
QTcpSocket.abort(self)
QTcpSocket.connectToHost(self, host, port)
def close(self):
print 'close!'
self.disconnectFromHost()
def send(self, data):
self.writeData('%s|%s' % (len(data), data))
def on_ready_read(self):
if self.bytesAvailable():
data = str(self.readAll())
while data:
if not self.wait_len and '|' in data:#new data and new message
self.wait_len , data = data.split('|',1)
if match('[0-9]+', self.wait_len) and (len(self.wait_len) <= MAX_WAIT_LEN) and data.startswith('{'):#okay, this is normal length
self.wait_len = int(self.wait_len)
self.temp = data[:self.wait_len]
data = data[self.wait_len:]
else:#oh, it was crap
self.wait_len , self.temp = '',''
return
elif self.wait_len:#okay, not new message, appending
tl= int(self.wait_len)-len(self.temp)
self.temp+=data[:tl]
data=data[tl:]
elif not self.wait_len and not '|' in data:#crap
return
if self.wait_len and self.wait_len == len(self.temp):#okay, full message
self.data_ready.emit(self.temp)
self.wait_len , self.temp = '',''
if not data:
return
def print_command(self,data):
print 'data!'
def get_sstate(self):
print self.state()
def on_error(self):
print 'error', self.errorString()
self.close()
self.connectToHost('dev.ulab.ru', 10000)
def on_disconnect(self):
print 'disconnected!'
def on_connected(self):
print 'connected!'
self.send(json.dumps(
{'command' : "operator_insite",
'password' : "376c43878878ac04e05946ec1dd7a55f",
'login' : "nsandr",
'version':unicode("1.2.9")}))
if __name__ == "__main__":
app = QCoreApplication(sys.argv)
main_socket = UpQSocket()
state_timer = QTimer()
state_timer.setInterval(1000)
state_timer.timeout.connect(main_socket.get_sstate)
state_timer.start()
main_socket.connectToHost('dev.ulab.ru', 10000)
sys.exit(app.exec_())
Here is output:
connectToHost
1
1
connected!
data!
data!
3
3
3
3
3
error The remote host closed the connection
close!
disconnected!
connectToHost
2
2
Workaround:
import functools
def on_error(self):
print 'error', self.errorString()
QTimer.singleShot(2000, functools.partial(self.connectToHost, 'localhost', 9999))
# 2000 - your prefered reconnect timeout in ms
Update
There is more correct solution in comments for Qt bugreport QTBUG-18082. Here is Python implementation:
#QtCore.pyqtSlot()
def do_reconnect(self):
print 'Trying to reconnect'
self.connectToHost('localhost', 9999)
def on_error(self):
print 'error', self.errorString()
QtCore.QMetaObject.invokeMethod(self, 'do_reconnect', QtCore.Qt.QueuedConnection)
or just:
QTimer.singleShot(0, self.do_reconnect) # or any callable, slot is unnecessary
which anyway will call QtCore.QMetaObject.invokeMethod with QueuedConnection conection type (source)