Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Getting URL query parameters on connection opened? #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stevenliebregt opened this issue Jun 21, 2018 · 4 comments
Open

Getting URL query parameters on connection opened? #53

stevenliebregt opened this issue Jun 21, 2018 · 4 comments

Comments

@stevenliebregt
Copy link

How can I retrieve URL query parameters in the on_connect method?

For example:

I have a client which connects using the following url: ws://localhost:5000?identification=web-client

How can I retrieve the identification parameter in the on_connect?

@mfranchitti
Copy link

Were you able to figure this out? I have a similar need.

@amattu2
Copy link

amattu2 commented Jan 20, 2019

Also in need of this. I'm not great with Python but I'm going to give it a shot, and see if I can manually add it.

Edit: I have finished the portion to grab the query string. It's now working as expected, I simply need to finish the code. I will update with the appropriate code within a day or two.

@amattu2
Copy link

amattu2 commented Jan 23, 2019

For sake of simplicity, I'll just post the updated code here. I haven't tested the code on a wide range of strings, right now if your query parameters contain more than alphanumeric characters, it could potentially break. I simply made this for my needs.

In order to add this feature, you will need to update two functions in the websocket_server.py source code.

  1. Find: def _new_client(self, handler) [Line 102] and replace it with the following code
	def _new_client_(self, handler):
		self.id_counter += 1
		client={
			'id'      : self.id_counter,
			'handler' : handler,
			'address' : handler.client_address,
			'query'   : handler.query
		}
		self.clients.append(client)
		self.new_client(client, self)
  1. Find: def handshake(self) [Line 242] and replace it with the following code
	def handshake(self):
		# Variables
		message = self.request.recv(1024).decode().strip()
		upgrade = re.search('\nupgrade[\s]*:[\s]*websocket', message.lower())
		key = re.search('\n[sS]ec-[wW]eb[sS]ocket-[kK]ey[\s]*:[\s]*(.*)\r\n', message)
		get_header = re.search('[\n]*[gG][eE][tT][\s]*[/?&=!#$a-zA-Z0-9]*', message)
		query = ""

		# Checks
		if not upgrade:
			self.keep_alive = False
			return
		if key:
			key = key.group(1)
		else:
			print("Client tried to connect but was missing a key")
			self.keep_alive = False
			return
		if get_header:
			get_header = get_header.group()
			query = re.sub('[gG][eE][tT][\s]*/', '', get_header)

		# Setup
		response = self.make_handshake_response(key)
		self.query = query
		self.handshake_done = self.request.send(response.encode())
		self.valid_client = True
		self.server._new_client_(self)

After you edit the websocket_server.py script, you can then access the query parameters via any callback that provides the client.

Eg:

def connect(client, server):
	print(client['query'])

@adityamohta
Copy link

I have created a PR, which can be a solution for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants