Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > URL emulator (sockets question)

Thread Tools

URL emulator (sockets question)

Posts: n/a
Hi All,

I writing a program to emulate a URL (essentially a very simple raw data proxy). The client should always think that it is only talking to the proxy and the proxy acts like the url it's emulating.

My *immediate* problem is how do I tell the client that the web server has finished sending?

I know that when you recieve 0 bytes from the web server, it means that it is done how do I indicate the same thing to the browser? I've tried various combos of 'shutdown' and 'close' but the browser (Opera) indicates a premature disconnection.

I was thinking of using it as a proxy between my cellphone and some wap sites I'm developing/debugging. I don't like to type out long urls on the phone and I figured this would be a good learning experience.

--- Code Start ---
import socket
import sys
import select
import re

IMG = re.compile(r'(src="[^"]+")')

class ProxyServer:
def __init__(self, port, url, rmtport):
self.port = port
self.url = url
self.rmtport = rmtport
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clients = {}
self.count = 0

def start(self):
self.socket.bind((socket.gethostname(), self.port))

while 1:
self.clisocket, self.clientaddress = self.socket.accept()
self.returnIP = self.clisocket.getsockname()
self.count += 1
except KeyboardInterrupt:

def proxy(self):
websvr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
websvr.connect((self.url, self.rmtport))

self.request = ''
self.response = ''

f = file("" % self.count, 'w')

while 1:
r, w, e =[self.clisocket, websvr],
[self.clisocket, websvr], [], 30)
# if client requests something
if self.clisocket in r:
request = self.clisocket.recv(1024)
f.write("\n--RAW REQUEST\n%s" % request)
self.request = self.transreq(request, f)
self.done = False
# if websvr writable and have someting to send
if websvr in w and self.request != '':
self.sendit(websvr, self.request)
self.request = ''
# if websvr readable and hasn't returned ''
if websvr in r and not self.done:
response = websvr.recv(1024)
if response == '':
self.done = True
self.response = self.transresp(response, f)
# if client writable and last response wasn't ''
if self.clisocket in w and self.response != '':
self.sendit(self.clisocket, self.response)
self.response = ''
# if self.done:
# What should I put here??
except socket.timeout:

self.count += 1

def sendit(self, sock, data):
l = len(data)
sent = 0
while sent < l:
sent += sock.send(data)
data = data[sent:]

def transreq(self, request, f=None):
request = request.replace("%s:%s" % self.returnIP, self.url)
return request

def transresp(self, response, f=None):
if f is not None:
f.write("\n--RAW RESPONSE\n%s" % response)
response = response.replace(self.url, "%s:%s" % self.returnIP)
response = IMG.sub('', response)
response = response.replace("\r\n0\r\n", "\r\n")
return response

if __name__ == "__main__":
wapsite = ""
svr = ProxyServer(9000, wapsite, 80)

--- END ---

Any suggestions/tips? Thanks,

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where download NetSYS Emulator? Andrea Cisco 4 12-11-2004 04:14 AM
Copy satellite assemblies to Windows CE emulator John ASP .Net 0 11-09-2004 11:21 AM
URL - substitution of a correct URL by a GUID like URL in favorites. Just D. ASP .Net Mobile 0 08-11-2004 04:26 PM
redirect URL's, return URL's, and URL Parameters Jon paugh ASP .Net 1 07-10-2004 05:29 AM
Cisco IP Phone Simulator / Emulator ? Fox Cisco 4 05-04-2004 07:36 PM