@@ -120,37 +120,38 @@ export default {
120120
121121``` py
122122import re
123- from js import Response, URL , fetch
123+ from cloudflare.workers import Response
124+ from urllib.parse import urlparse
124125
125126async def on_fetch (request ):
126127 blocked_hostnames = [" nope.mywebsite.com" , " bye.website.com" ]
127- url = URL .new (request.url)
128+ url = urlparse (request.url)
128129
129130 # Block on hostname
130131 if url.hostname in blocked_hostnames:
131- return Response.new (" Blocked Host" , status = 403 )
132+ return Response(" Blocked Host" , status = 403 )
132133
133134 # On paths ending in .doc or .xml
134- if re.search(r ' \. ( doc| xml) $ ' , url.pathname ):
135- return Response.new (" Blocked Extension" , status = 403 )
135+ if re.search(r ' \. ( doc| xml) $ ' , url.path ):
136+ return Response(" Blocked Extension" , status = 403 )
136137
137138 # On HTTP method
138139 if " POST" in request.method:
139- return Response.new (" Response for POST" )
140+ return Response(" Response for POST" )
140141
141142 # On User Agent
142143 user_agent = request.headers[" User-Agent" ] or " "
143144 if " bot" in user_agent:
144- return Response.new (" Block User Agent containing bot" , status = 403 )
145+ return Response(" Block User Agent containing bot" , status = 403 )
145146
146147 # On Client's IP address
147148 client_ip = request.headers[" CF-Connecting-IP" ]
148149 if client_ip == " 1.2.3.4" :
149- return Response.new (" Block the IP 1.2.3.4" , status = 403 )
150+ return Response(" Block the IP 1.2.3.4" , status = 403 )
150151
151152 # On ASN
152153 if request.cf and request.cf.asn == 64512 :
153- return Response.new (" Block the ASN 64512 response" )
154+ return Response(" Block the ASN 64512 response" )
154155
155156 # On Device Type
156157 # Requires Enterprise "CF-Device-Type Header" zone setting or
@@ -159,7 +160,7 @@ async def on_fetch(request):
159160 if device == " mobile" :
160161 return Response.redirect(" https://mobile.example.com" )
161162
162- return fetch(request )
163+ return Response( " Request Allowed " )
163164```
164165
165166</TabItem > </Tabs >
0 commit comments