473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

URL emulator (sockets question)

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 sending...so 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))
self.socket.listen(5)

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

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

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

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

while 1:
try:
r, w, e = select.select([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
else:
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:
pass

self.count += 1
f.close()

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 = "www.somewapsite.com"
svr = ProxyServer(9000, wapsite, 80)
svr.start()

--- END ---

Any suggestions/tips? Thanks,

pythonhda
Jul 18 '05 #1
0 1476

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Irvine | last post by:
Hi, I recently upgraded to anAMD64 3400 and installed XP SP2. Unpon trying to run a smart application on the emulator, I recieved and error message: "Emulator for Windows CE One or more...
2
by: maderjc | last post by:
I got a new PC and installed Microsoft Visual Studio .Net 2003 and then installed "Microsoft Pocket PC 2003 SDK.msi" and "Windows Mobile 2003 Second Edition Emulator Images for Pocket PC - WWE.msi"...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
2
by: Schraalhans Keukenmeester | last post by:
After searching for an emulator for the typical cellphone/handheld java applets (do these have a specific name like beans or something?) I arrived at Sun's website and found a download for a...
15
by: Douglas Garstang | last post by:
All, I posted a newsgroup question here a few weeks back, asking some questions that related to my 10 year quest (so far) to understand pointers. Someone suggested I write a simple emulator....
2
by: Claire | last post by:
Im running a demo database application for a pocketpc device. When it runs in debug mode in the emulator I notice that it also downloads the associated database file with it. I'm using c# in Visual...
2
by: Stuart Ferguson | last post by:
Hi All, I am currently developing software for a Windows CE PC which i currently dont have the hardware, thus I am using the emulator for VS2003. The software writes out Timesheet entries to...
0
by: MPulse | last post by:
We are in the final stages of developing a client for our web service on the Pocket PC/Windows Mobile platform. We are running into a problem with how our sales/distributors can demonstrate this...
4
by: mac | last post by:
I have a petty basic machine: Win XP 1GB RAM AMD 3500+ Chip (2.2 Mhz) I just did a single form "Hello World" application and tried to debug. It took 20 seconds to deploy to the emulator. The...
0
by: srinathava | last post by:
Hi, I am trying to send keystrokes to the NES emulator nester. The idea was to be able to use a standard NES controller whose "state" I can read in using Python and then conveying that to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.